In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.