LeetCode——树:递归
生活随笔
收集整理的這篇文章主要介紹了
LeetCode——树:递归
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
LeetCode——樹:遞歸
目錄
0. 概述
1. 樹的高度(LeetCode104)
1. 概述
2. 思路
3. 代碼
public static int maxDepth1(TreeNode root) {if (root == null) {return 0;}int left = maxDepth(root.left);int right = maxDepth(root.right);return Math.max(left, right) + 1;}2. 平衡樹(LeetCode110)
1. 思路
2. 代碼
private static boolean result = true;public static boolean isBalanced(TreeNode root) {maxDepth(root);return result;}private static int maxDepth(TreeNode root) {if (root == null) {return 0;}int l = maxDepth(root.left);int r = maxDepth(root.right);if (Math.abs(l - r) > 1) {result = false;}return Math.max(l, r) + 1;}3. 兩節(jié)點的最長路徑(LeetCode543)
1. 概述
2. 思路
3. 代碼
private static int max = 0;public static int diameterOfBinaryTree(TreeNode root) {maxDepth(root);return max;}private static int maxDepth(TreeNode root) {if (root == null) {return 0;}int l = maxDepth(root.left);int r = maxDepth(root.right);max = Math.max(max, l + r);return Math.max(l, r) + 1;}4. 翻轉(zhuǎn)樹(LeetCode226)
1. 概述
2. 思路
3. 代碼
public static TreeNode invertTree(TreeNode root) {if (root == null) {return null;}TreeNode left = invertTree(root.left);TreeNode right = invertTree(root.right);root.left = right;root.right = left;return root;}5. 歸并兩棵樹(LeetCode617)
1. 概述
2. 思路
3. 代碼
public static TreeNode mergeTrees(TreeNode t1, TreeNode t2) {if (t1 == null && t2 == null)return null;if (t1 == null)return t2;if (t2 == null)return t1;TreeNode root = new TreeNode(t1.val + t2.val);root.left = mergeTrees(t1.left, t2.left);root.right = mergeTrees(t1.right, t2.right);return root;}6. 判斷路徑和是否等于一個數(shù)(LeetCode112)
1. 概述
2. 思路
3. 代碼
public static boolean hasPathSum(TreeNode root, int sum){if (root==null){return false;}sum -= root.val;if (root.left==null&&root.right==null){return sum==0;}return hasPathSum(root.left,sum)||hasPathSum(root.right,sum);}7. 統(tǒng)計路徑和等于一個數(shù)的路徑數(shù)量(LeetCode437)
1. 概述
2. 思路
3. 代碼
public static int pathSum(TreeNode root, int sum) {if (root == null) {return 0;}return pathSumStartWithRoot(root, sum) + pathSum(root.left, sum) + pathSum(root.right, sum);}private static int pathSumStartWithRoot(TreeNode root, int sum) {if (root == null) {return 0;}int ret = 0;if (root.val == sum)ret++;ret += pathSumStartWithRoot(root.left, sum - root.val) + pathSumStartWithRoot(root.right, sum - root.val);return ret;}8. 子樹(LeetCode572)
1. 概述
2. 思路
1. 如果tnull&&snull,return true
2. 如果tnull||snull,return false
3. 如果t.val!=s.val,return false
4. 再比較它們的左右節(jié)點是否相同
3. 代碼
public static boolean isSubtree(TreeNode s, TreeNode t) {if (s == null && t == null) {return true;}if (s == null) {return false;}return isSubtreeWithRoot(s, t) || isSubtree(s.left, t) || isSubtree(s.right, t);}private static boolean isSubtreeWithRoot(TreeNode s, TreeNode t) {if (s == null && t == null) {return true;}if (s == null || t == null) {return false;}if (t.val != s.val) {return false;}return isSubtreeWithRoot(s.left, t.left) && isSubtreeWithRoot(s.right, t.right);}9. 樹的對稱(LeetCode101)
1. 概述
2. 思路
3. 代碼
public static boolean isSymmetric(TreeNode root) {if (root == null) {return true;}return isSymmetric(root.left, root.right);}public static boolean isSymmetric(TreeNode t1, TreeNode t2) {if (t1 == null && t2 == null)return true;if (t1 == null || t2 == null)return false;if (t1.val != t2.val)return false;return isSymmetric(t1.left, t2.right) && isSymmetric(t1.right, t2.left);}10. 最小深度(LeetCode111)
1. 概述
2. 思路
1. 利用遞歸,假設(shè)可以得到root的左右孩子的高度,那么最小深度就是左右孩子高度較小的那個加上自身高度,即return Math.min(left, right)+12. 而root的左右孩子也能看出一顆完整的樹,有自己左右孩子高度,依次遞歸3. 當左右孩子為null時,高度就是自己。當左右孩子有一個為null時,高度就是不為null的高度加上自己。即if(left==0||right==0) return left+right+13. 代碼
public static int minDepth(TreeNode root) {if (root == null) {return 0;}int left = minDepth(root.left);int right = minDepth(root.right);if (left == 0 || right == 0) {return left + right + 1;}return Math.min(left, right) + 1;}11. 統(tǒng)計左葉子節(jié)點的和(LeetCode404)
1. 概述
2. 思路
3. 代碼
public static int sumOfLeftLeaves(TreeNode root) {if (root == null) {return 0;}if (isLeaf(root.left))return root.left.val + sumOfLeftLeaves(root.right);return sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right);}private static boolean isLeaf(TreeNode node) {if (node == null)return false;return node.left == null && node.right == null;}12. 相同節(jié)點值的最大路徑長度(LeetCode687)
1. 概述
2. 思路
int right = dfs(root.right),能獲取到右節(jié)點最長同值長度
3. 代碼
private static int path = 0;public static int longestUnivaluePath(TreeNode root) {dfs(root);return path;}private static int dfs(TreeNode root) {if (root == null)return 0;int left = dfs(root.left);int right = dfs(root.right);int leftPath = root.left != null && root.left.val == root.val ? left + 1 : 0;int rightPath = root.right != null && root.right.val == root.val ? right + 1 : 0;path = Math.max(path, leftPath + rightPath);return Math.max(leftPath, rightPath);}13. 間隔遍歷(LeetCode337)
1. 概述
2. 思路
1. 如果算上root本身的值root.val,如果左子樹不為null,則再加上左子樹左右節(jié)點的值,右子樹同理。
2. 如果不算上root本身的值,則是rob(root.left)+rob(root.right)的值
3. rob函數(shù)表示以當前節(jié)點為根,能搶到最大的值
3. 代碼
public static int rob(TreeNode root){if (root==null)return 0;int val1 = root.val;if (root.left!=null)val1+=rob(root.left.left)+rob(root.left.right);if (root.right!=null)val1+= rob(root.right.left)+rob(root.right.right);int val2 = rob(root.left)+rob(root.right);return Math.max(val1,val2);}14. 小結(jié)
int left = minDepth(root.left);
int right = minDepth(root.right);
總結(jié)
以上是生活随笔為你收集整理的LeetCode——树:递归的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeetCode——链表
- 下一篇: 第二章 Spark RDD以及编程接口