Last updated on 2023年7月16日 下午
513.找树左下角的值(递归写法) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 /** * 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 max_depth = -1 ; int result = 0 ; public void huisu(TreeNode root, int depth){ if (root.left == null && root.right == null ){ if (max_depth < depth){ max_depth = depth; result = root.val ; } return ; } if (root.left != null ){ depth++ ; huisu(root.left, depth); depth-- ; } if (root.right != null ){ depth++ ; huisu(root.right, depth); depth-- ; } } public int findBottomLeftValue(TreeNode root) { huisu(root, 0 ); return result; } }
513.找树左下角的值(迭代法) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 class Solution { public int findBottomLeftValue(TreeNode root) { int result = 0 ; Queue<TreeNode> que = new LinkedList<>(); que.offer(root); while (!que.isEmpty()){ int size = que.size (); for (int i = 0 ;i < size ;i ++){ TreeNode node = que.poll(); if (i == 0 ){ result = node.val; } if (node.left != null ){ que.offer(node.left); } if (node.right != null ){ que.offer(node.right); } } } return result; } }
112.路经总和(递归法) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 class Solution { public boolean huisu(TreeNode root, int currSum){ if (root.left == null && root.right == null && currSum == 0 ){ return true ; } if (root.left == null && root.right == null ){ return false ; } if (root.left != null ){ currSum -= root.left.val ; if (huisu(root.left, currSum)) return true ; currSum += root.left.val ; } if (root.right != null ){ currSum -= root.right.val ; if (huisu(root.right, currSum)) return true ; currSum += root.right.val ; } return false ; } public boolean hasPathSum(TreeNode root, int targetSum) { if (root == null ){ return false ; } return huisu(root, targetSum - root.val ); } }
112.路径总和(迭代法) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 public class MyNode{ TreeNode treeNode; int currSum = 0 ; MyNode(TreeNode root , int currSum ) { this.treeNode = root; this.currSum = currSum; } }class Solution { public boolean hasPathSum(TreeNode root , int targetSum ) { if (root == null){ return false ; } Stack<MyNode> st = new Stack<>() ; MyNode rootNode = new MyNode(root , root .val ) ; st.push(rootNode); while (!st.isEmpty() ){ MyNode node = st.peek() ; st.pop() ; if (targetSum == node.currSum && node.treeNode.left == null && node.treeNode.right == null){ return true ; } if (node.treeNode.right != null){ st.push(new MyNode(node .treeNode .right , node .currSum + node .treeNode .right .val ) ); } if (node.treeNode.left != null){ st.push(new MyNode(node .treeNode .left , node .currSum + node .treeNode .left .val ) ); } } return false ; } }
113.路经总和2(回溯法) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 class Solution { //使用回溯方法做 public void huisu(TreeNode root, int targetSum, List<List<Integer >> result, List<Integer > path ){ if (root == null ){ return ; } path .add (root.val); if (root.left == null && root.right == null && targetSum == root.val){ result.add (new ArrayList(path )); } huisu(root.left, targetSum - root.val, result, path ); huisu(root.right, targetSum - root.val, result, path ); path .remove(path .size() - 1 ); } public List<List<Integer >> pathSum(TreeNode root, int targetSum) { List<List<Integer >> result = new ArrayList<>(); List<Integer > path = new ArrayList<>(); huisu(root, targetSum, result, path ); return result; } }
113.路经总和2(DFS法) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 class Solution { List<List<Integer >> res = new ArrayList<>(); public List<List<Integer >> pathSum(TreeNode root, int sum ) { List<Integer > cur = new ArrayList<>(); dfs(root, cur, 0 , sum ); return res; } public void dfs(TreeNode node, List<Integer > cur, int sum , int target ){ if (node == null){ return ; } if (node.left == null && node.right == null && node.val + sum == target ){ cur.add(node.val); res.add(new ArrayList<>(cur)); cur.remove(cur.size () - 1 ); return ; } cur.add(node.val); dfs(node.left, cur, sum + node.val, target ); dfs(node.right, cur, sum + node.val, target ); cur.remove(cur.size () - 1 ); } }
106. 从中序与后序遍历序列构造二叉树 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 class Solution { public TreeNode buildTree(int [] inorder , int [] postorder ) { int in_length = inorder.length; int post_length = postorder.length; if (in_length == 0 || post_length == 0 ){ return null; } int root_val = postorder[post_length - 1 ] ; TreeNode root = new TreeNode(root_val ) ; int k = 0 ; for (int i = 0 ; i < in_length;i++){ if (root_val == inorder[i ] ){ k = i; break; } } int [] left_in = Arrays . copyOfRange(inorder , 0, k ) ; int [] left_post = Arrays . copyOfRange(postorder , 0, k ) ; root.left = buildTree(left_in , left_post ) ; int [] right_in = Arrays . copyOfRange(inorder , k + 1, in_length ) ; int [] right_post = Arrays . copyOfRange(postorder , k , post_length - 1) ; root.right = buildTree(right_in , right_post ) ; return root; } }
105. 从前序与中序遍历序列构造二叉树 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 class Solution { public TreeNode buildTree(int [] preorder , int [] inorder ) { int pre_length = preorder.length; int in_length = inorder.length; if (pre_length == 0 || in_length == 0 ){ return null; } int root_val = preorder[0 ] ; TreeNode root = new TreeNode(root_val ) ; int k = 0 ; for (int i = 0 ;i < in_length;i++){ if (root_val == inorder[i ] ){ k = i; break; } } int [] left_pre = Arrays . copyOfRange(preorder , 1, k + 1) ; int [] left_in = Arrays . copyOfRange(inorder , 0, k ) ; root.left = buildTree(left_pre , left_in ) ; int [] right_pre = Arrays . copyOfRange(preorder , k + 1, pre_length ) ; int [] right_in = Arrays . copyOfRange(inorder , k + 1, in_length ) ; root.right = buildTree(right_pre , right_in ) ; return root; } }
654. 最大二叉树 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 class Solution { public TreeNode constructMaximumBinaryTree(int [] nums ) { int n_length = nums.length; if (n_length == 0 ){ return null; } int max_value = -1 ; int k = 0 ; for (int i = 0 ;i < nums.length;i++){ if (max_value < nums[i ] ){ max_value = nums[i ] ; k = i; } } TreeNode root = new TreeNode(max_value ) ; int [] left_nums = Arrays . copyOfRange(nums , 0, k ) ; int [] right_nums = Arrays . copyOfRange(nums , k + 1, n_length ) ; root.left = constructMaximumBinaryTree(left_nums ) ; root.right = constructMaximumBinaryTree(right_nums ) ; return root; } }
617. 合并二叉树(递归实现) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 /** * 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 huisu(TreeNode root1, TreeNode root2){ if (root1 == null){ return root2; } if (root2 == null){ return root1; } root1.val += root2.val; root1.left = huisu(root1.left , root2.left ); root1.right = huisu(root1.right , root2.right ); return root1; } public TreeNode mergeTrees(TreeNode root1, TreeNode root2) { return huisu(root1, root2); } }
617. 合并二叉树(迭代实现) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 /** * 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 mergeTrees(TreeNode root1, TreeNode root2) { if (root1 == null ){ return root2; } if (root2 == null ){ return root1; } Queue< TreeNode> que = new LinkedList<> (); que.offer(root1); que.offer(root2); while (! que.isEmpty()){ TreeNode node1 = que.poll(); TreeNode node2 = que.poll(); node1.val += node2.val ; if (node1.left != null && node2.left != null ){ que.offer(node1.left); que.offer(node2.left); } if (node1.right != null && node2.right != null ){ que.offer(node1.right); que.offer(node2.right); } if (node1.left == null && node2.left != null ){ node1.left = node2.left; } if (node1.right == null && node2.right != null ){ node1.right = node2.right; } } return root1; } }
my-leetcode-logs-20230710.md
https://thewangyang.github.io/2023/07/10/leetcode-notes-20230710/