赞
踩
没啥好说的,使用队列,这里注意java也使用deque进行模拟,这里总结下deque用法:
class Solution { private List<List<Integer>> res = new ArrayList<>(); public List<List<Integer>> levelOrder(TreeNode root) { if(root == null){ return res; } Deque<TreeNode> deque = new LinkedList<>(); deque.add(root); while(!deque.isEmpty()){ int len = deque.size(); List<Integer> res1 = new ArrayList<>(); for (int i = 0; i < len; i++){ TreeNode p = deque.peek(); res1.add(p.val); deque.poll(); if(p.left != null){ deque.add(p.left); } if(p.right != null){ deque.add(p.right); } } res.add(res1); } return res; } }
就是从下往上输出,这里把层序遍历的res数组按第一维度翻转即可:
class Solution { private List<List<Integer>> res = new ArrayList<>(); public List<List<Integer>> levelOrderBottom(TreeNode root) { if (root == null) { return res; } Deque<TreeNode> deque = new LinkedList<>(); deque.add(root); while (!deque.isEmpty()) { int len = deque.size(); List<Integer> res1 = new ArrayList<>(); for (int i = 0; i < len; i++) { TreeNode p = deque.peek(); res1.add(p.val); deque.poll(); if (p.left != null) { deque.add(p.left); } if (p.right != null) { deque.add(p.right); } } res.add(res1); } Collections.reverse(res); return res; } }
只把每一层的最后一个节点加入结果集即可。
class Solution { private List<Integer> res = new ArrayList<>(); public List<Integer> rightSideView(TreeNode root) { if(root == null){ return res; } Deque<TreeNode> deque = new LinkedList<>(); deque.add(root); while(!deque.isEmpty()){ int len = deque.size(); for (int i = 0; i < len; i++){ TreeNode p = deque.peek(); deque.poll(); if(p.left != null){ deque.add(p.left); } if(p.right != null){ deque.add(p.right); } if(i == len - 1){ res.add(p.val); } } } return res; } }
class Solution { private List<Double> res = new ArrayList<>(); public List<Double> averageOfLevels(TreeNode root) { if(root == null){ return res; } Deque<TreeNode> deque = new LinkedList<>(); deque.add(root); while(!deque.isEmpty()){ int len = deque.size(); double sum = 0.0; for (int i = 0; i < len; i++){ TreeNode p = deque.peek(); sum += p.val; deque.poll(); if(p.left != null){ deque.add(p.left); } if(p.right != null){ deque.add(p.right); } } res.add(sum / len); } return res; } }
把p.left p.right换成for循环遍历children数组。
class Solution { private List<List<Integer>> res = new ArrayList<>(); public List<List<Integer>> levelOrder(Node root) { if (root == null) { return res; } Deque<Node> deque = new LinkedList<>(); deque.add(root); while (!deque.isEmpty()) { int len = deque.size(); List<Integer> res1 = new ArrayList<>(); for (int i = 0; i < len; i++) { Node p = deque.peek(); res1.add(p.val); deque.poll(); for (int j = 0; j < p.children.size(); j++) { if (p.children.get(j) != null) { deque.add(p.children.get(j)); } } } res.add(res1); } return res; } }
跟求平均值那题一样的思路:
class Solution { private List<Integer> res = new ArrayList<>(); public List<Integer> largestValues(TreeNode root) { if(root == null){ return res; } Deque<TreeNode> deque = new LinkedList<>(); deque.add(root); while(!deque.isEmpty()){ int len = deque.size(); int max = Integer.MIN_VALUE; for (int i = 0; i < len; i++){ TreeNode p = deque.peek(); max = Math.max(max , p.val); deque.poll(); if(p.left != null){ deque.add(p.left); } if(p.right != null){ deque.add(p.right); } } res.add(max); } return res; } }
跟右视图那题一样,最后一个节点特殊处理,其他的练到poll后队列的头部。
class Solution { public Node connect(Node root) { if(root == null){ return null; } Deque<Node> deque = new LinkedList<>(); deque.add(root); root.next = null; while(!deque.isEmpty()){ int len = deque.size(); for (int i = 0; i < len; i++) { Node p = deque.peek(); deque.poll(); p.next = deque.peek(); if(i == len - 1){ p.next = null; } if(p.left != null){ deque.add(p.left); } if(p.right != null){ deque.add(p.right); } } } return root; } }
通过117.填充每个节点的下一个右侧节点指针II我们可以得到,即使不是完美二叉树也可使用上面的代码,因为在层序遍历中,以我们的上帝视角看,其实结构还是一样的,都处于同一层。
层序遍历:用一个depth变量记录遍历的层数:
class Solution { public int maxDepth(TreeNode root) { if (root == null) { return 0; } int depth = 0; Deque<TreeNode> deque = new LinkedList<>(); deque.add(root); while (!deque.isEmpty()) { int len = deque.size(); depth++; for (int i = 0; i < len; i++) { TreeNode p = deque.peek(); deque.poll(); if (p.left != null) { deque.add(p.left); } if (p.right != null) { deque.add(p.right); } } } return depth; } }
一碰到叶子结点就停止输出:
class Solution { public int minDepth(TreeNode root) { if(root == null){ return 0; } int depth = 0; Deque<TreeNode> deque = new LinkedList<>(); deque.add(root); while(!deque.isEmpty()){ int len = deque.size(); depth++; List<Integer> res1 = new ArrayList<>(); for (int i = 0; i < len; i++){ TreeNode p = deque.peek(); deque.poll(); if(p.left == null && p.right == null){ return depth; } if(p.left != null){ deque.add(p.left); } if(p.right != null){ deque.add(p.right); } } } return depth; } }
递归法:
class Solution { private TreeNode traversal(TreeNode node){ if(node == null){ return null; } TreeNode left = traversal(node.left); TreeNode right = traversal(node.right); node.left = right; node.right = left; return node; } public TreeNode invertTree(TreeNode root) { traversal(root); return root; } }
迭代法:
public TreeNode invertTree(TreeNode root) { Deque<TreeNode> deque = new LinkedList<>(); if(root == null){ return null; } deque.push(root); while(!deque.isEmpty()){ TreeNode p = deque.peek(); deque.pop(); TreeNode tmp = p.right; p.right = p.left; p.left = tmp; if(p.right != null){ deque.push(p.right); } if(p.left != null){ deque.push(p.left); } } return root; }
class Solution { private int getHeight(TreeNode node){ if(node == null){ return 0; } int left = getHeight(node.left); if(left==-1){ return -1; } int right = getHeight(node.right); if(right == -1){ return -1; } if(Math.abs(left - right) > 1){ return -1; } return Math.max(left, right) + 1; } public boolean isBalanced(TreeNode root) { if(root == null){ return true; } return getHeight(root) != -1; } }
但leetcode中强调的深度和高度很明显是按照节点来计算的,如图:
(高度:从下往上,深度:从上往下)
关于根节点的深度究竟是1 还是 0,不同的地方有不一样的标准,leetcode的题目中都是以节点为一度,即根节点深度是1。但维基百科上定义用边为一度,即根节点的深度是0,我们暂时以leetcode为准(毕竟要在这上面刷题)。
class Solution { public boolean check(TreeNode left, TreeNode right){ if(left == null && right == null) { return true; }else if(left == null || right == null){ return false; }else if(left.val != right.val){ return false; } else{ return check(left.left, right.right) && check(left.right, right.left); } } public boolean isSymmetric(TreeNode root) { return check(root, root); } }
public boolean isSymmetric(TreeNode root) { if(root == null){ return true; } Deque<TreeNode> deque = new LinkedList<>(); deque.add(root.left); deque.add(root.right); while(!deque.isEmpty()){ TreeNode p1 = deque.peek(); deque.poll(); TreeNode p2 = deque.peek(); deque.poll(); if(p1 == null && p2 == null){ continue; }else if(p1 == null || p2 == null){ return false; }else if(p1.val != p2.val){ return false; }else{ deque.add(p1.left); deque.add(p2.right); deque.add(p1.right); deque.add(p2.left); } } return true; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。