What are the exercises of binary tree in Java algorithm
Editor to share with you what are the exercises of the binary tree in the Java algorithm, I believe most people do not know much about it, so share this article for your reference. I hope you will gain a lot after reading this article. Let's learn about it together.
Topic one
Solution method
/ * Definition for a binary tree node. * public class TreeNode {* int val; * TreeNode left; * TreeNode right; * TreeNode () {} * TreeNode (int val) {this.val = val;} * TreeNode (int val, TreeNode left, TreeNode right) {* this.val = val; * this.left = left; * this.right = right; *} * * / class Solution {int I = 0 Int res = 0; public int kthSmallest (TreeNode root, int k) {method (root,k); return res;} public void method (TreeNode root, int k) {if (root==null) return; method (root.left,k); iTunes; if (iComplek) {res = root.val; return;} method (root.right,k) Question 2
Solution method
/ * Definition for a binary tree node. * public class TreeNode {* int val; * TreeNode left; * TreeNode right; * TreeNode () {} * TreeNode (int val) {this.val = val;} * TreeNode (int val, TreeNode left, TreeNode right) {* this.val = val; * this.left = left; * this.right = right; *} * * / class Solution {int sum = 0 Public TreeNode convertBST (TreeNode root) {method (root); return root;} public void method (TreeNode root) {if (root==null) {return;} method (root.right); sum+=root.val; root.val = sum; method (root.left);}} topic 3
Solution method
/ * Definition for a binary tree node. * public class TreeNode {* int val; * TreeNode left; * TreeNode right; * TreeNode () {} * TreeNode (int val) {this.val = val;} * TreeNode (int val, TreeNode left, TreeNode right) {* this.val = val; * this.left = left; * this.right = right * / class Solution {public boolean isValidBST (TreeNode root) {return method (root,null,null);} public boolean method (TreeNode root,TreeNode min,TreeNode max) {if (root==null) return true; if (mininghouse nullable root.validate max.val) return false; return method (root.left,min,root) & & method (root.right,root,max);}} topic 4
Solution method
/ * Definition for a binary tree node. * public class TreeNode {* int val; * TreeNode left; * TreeNode right; * TreeNode () {} * TreeNode (int val) {this.val = val;} * TreeNode (int val, TreeNode left, TreeNode right) {* this.val = val; * this.left = left; * this.right = right * / class Solution {public TreeNode searchBST (TreeNode root, int val) {if (root==null) return null; if (root.val==val) return root; if (root.val > = val) {return searchBST (root.left,val);} if (root.val val) root.left = insertIntoBST (root.left,val); return root;}} question 6
Arithmetic
/ * Definition for a binary tree node. * public class TreeNode {* int val; * TreeNode left; * TreeNode right; * TreeNode () {} * TreeNode (int val) {this.val = val;} * TreeNode (int val, TreeNode left, TreeNode right) {* this.val = val; * this.left = left; * this.right = right * / class Solution {public TreeNode deleteNode (TreeNode root, int key) {if (root = = null) return null; if (root.val = = key) {if (root.left = = null) return root.right; if (root.right = = null) return root.left; TreeNode minNode = getMin (root.right); root.right = deleteNode (root.right, minNode.val) MinNode.left = root.left; minNode.right = root.right; root = minNode;} else if (root.val > key) {root.left = deleteNode (root.left,key);} else {root.right = deleteNode (root.right,key);} return root } TreeNode getMin (TreeNode node) {while (node.left! = null) node = node.left; return node;}} above are all the contents of this article entitled "what are the exercises of binary trees in Java algorithm?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!