404
按理说也可以递归做。
public static int sumOfLeftLeaves(TreeNode root) { int total = 0; LinkedList<TreeNode> stack = new LinkedList<>(); stack.push(root); while (!stack.isEmpty()){ TreeNode pop = stack.pop(); TreeNode left = pop.left; if (left != null){ if (left.left == null && left.right == null){ total+=left.val; } pop.left = null; stack.push(left); } TreeNode right = pop.right; if (right != null){ pop.right = null; stack.push(right); } } return total; }
530
比较low的做法是像我这样
class Solution { public int getMinimumDifference(TreeNode root) { List<Integer> list = new ArrayList<>(); find(root,list); int min = Integer.MAX_VALUE; for (int i = 1; i < list.size(); i++) { min = Math.min(list.get(i)-list.get(i-1),min); } return min; } private void find(TreeNode node,List<Integer> list){ if (node.left != null){ find(node.left,list); } list.add(node.val); if (node.right != null){ find(node.right,list); } } }
这儿看了下官解是用了临时变量来做 效率会好很多
class Solution { int min=100000000,p,q=-100000000; public int getMinimumDifference(TreeNode root) { if(root==null){ return 0; } getMinimumDifference(root.left); p = root.val; if((p-q)<min){ min = p-q; } q = p; getMinimumDifference(root.right); return min; } } 作者:pu-ti-shu-xia-n 链接:https://leetcode-cn.com/problems/minimum-absolute-difference-in-bst/solution/zhe-yao-bao-li-ni-que-ding-bu-yong-ma-by-n002/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
543
class Solution { int max; public int diameterOfBinaryTree(TreeNode root) { if(root == null){ return 0; } max = Integer.MIN_VALUE; deep(root,0); return max; } private int deep(TreeNode node,int current){ if (node == null){ return current-1; } int ld = deep(node.left, current + 1); int rd = deep(node.right, current + 1); max = Math.max((ld-current)+(rd-current),max); return Math.max(ld,rd); } }
559
class Solution { int max; public int maxDepth(Node root) { max = 0; if(root != null){ handle(root,1); } return max; } public void handle(Node root,int level){ max = Math.max(max,level); if(root.children != null){ for(Node n : root.children){ handle(n,level+1); } } } }
563 树的坡度
class Solution { int total; public int findTilt(TreeNode root) { if(root == null){ return 0; } total = 0; deep(root); return total; } private int deep(TreeNode node){ if (node == null){ return 0; } int deepL = deep(node.left); int deepR = deep(node.right); total+= Math.abs(deepR-deepL); return deepL+deepR+node.val; } }
572 子树
class Solution { public boolean isSubtree(TreeNode s, TreeNode t) { if (s == null || t == null){ return false; } return handle(s,t); } private static boolean handle(TreeNode s, TreeNode t){ if (s.val == t.val&&check(s,t)){ return true; } if (s.left != null && handle(s.left,t)){ return true; } if (s.right != null && handle(s.right,t)){ return true; } return false; } private static boolean check(TreeNode s, TreeNode t){ if (s == null && t == null){ return true; } if ((s==null && t != null) || (s!=null && t == null)){ return false; } if (s.val != t.val){ return false; } return check(s.left,t.left)&&check(s.right,t.right); } }
589
class Solution { public List<Integer> preorder(Node root) { List<Integer> result = new ArrayList<>(); if (root != null) fill(root,result); return result; } private void fill(Node root,List<Integer> result){ result.add(root.val); if (root.children!=null){ for (Node child : root.children) { fill(child,result); } } } }
迭代也就是利用栈来保存下,贴一份官解
class Solution { public List<Integer> preorder(Node root) { LinkedList<Node> stack = new LinkedList<>(); LinkedList<Integer> output = new LinkedList<>(); if (root == null) { return output; } stack.add(root); while (!stack.isEmpty()) { Node node = stack.pollLast(); output.add(node.val); Collections.reverse(node.children); for (Node item : node.children) { stack.add(item); } } return output; } } 作者:LeetCode 链接:https://leetcode-cn.com/problems/n-ary-tree-preorder-traversal/solution/ncha-shu-de-qian-xu-bian-li-by-leetcode/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
617 合并二叉树
class Solution { public TreeNode mergeTrees(TreeNode root1, TreeNode root2) { TreeNode result = new TreeNode(); if (root1 == null || root2 == null){ result = root1 == null?root2:root1; }else { meage(result,root1,root2); } return result; } public static void meage(TreeNode root,TreeNode root1,TreeNode root2){ root.val = root1.val+root2.val; if (root1.left != null && root2.left != null){ TreeNode lefs = new TreeNode(); root.left = lefs; meage(lefs,root1.left,root2.left); }else { root.left = root1.left == null?root2.left:root1.left; } if (root1.right != null && root2.right != null){ TreeNode lefs = new TreeNode(); root.right = lefs; meage(lefs,root1.right,root2.right); }else { root.right = root1.right == null?root2.right:root1.right; } } }
637 此题用数组存数据应该效率能提升很多
int max; Map<Integer,List<Integer>> recoreds; public List<Double> averageOfLevels(TreeNode root) { List<Double> result = new ArrayList<>(); if (root == null)return result; max = 0; recoreds = new HashMap<>(); find(root,1); for (int i = 1; i <= max; i++) { //double asDouble = recoreds.get(i).stream().mapToDouble(Number::doubleValue).average().getAsDouble(); int total = 0; List<Integer> integers = recoreds.get(i); for (Integer integer : integers) { total+=integer; } result.add((double)(total/integers.size())); } return result; } public void find(TreeNode node,int level){ if (node == null)return; max = Math.max(level,max); List<Integer> rec = recoreds.get(level); if (rec == null){ rec = new ArrayList<>(); recoreds.put(level,rec); } rec.add(node.val); find(node.left,level+1); find(node.right,level+1); }
653 两数之和
class Solution { Set<Integer> set; boolean exist = false; int s; public boolean findTarget(TreeNode root, int k) { s = k;exist = false;set = new HashSet<>(); find(root); return exist; } public void find(TreeNode root){ if (exist || root == null){ return ; } if (set.contains(s-root.val)){ exist = true; return; }else { set.add(root.val); find(root.left); find(root.right); } } }
700 二叉树搜索
class Solution { public TreeNode searchBST(TreeNode root, int val) { if (root != null ){ if (root.val == val){ return root; }else if (root.val>val){ return searchBST(root.left,val); }else { return searchBST(root.right,val); } } return null; } }