Problem
Given a binary tree, return the level order traversal of its nodes" values. (ie, from left to right, level by level).
For example:
Given binary tree {3,9,20,#,#,15,7},
3 / 9 20 / 15 7
return its level order traversal as:
[ [3], [9,20], [15,7] ]Note
用先入先出的Queue。用size标记每一层的结点数目并定向循环。
Solutionclass Solution { public List> levelOrder(TreeNode root) { List
> res = new ArrayList<>(); if (root == null) return res; Queue
queue = new LinkedList (); queue.offer(root); while (!queue.isEmpty()) { int size = queue.size(); List temp = new ArrayList<>(); for (int i = 0; i < size; i++) { TreeNode cur = queue.poll(); if (cur.left != null) queue.offer(cur.left); if (cur.right != null) queue.offer(cur.right); temp.add(cur.val); } res.add(temp);//for traversal II: res.add(0, temp); } return res; } }
DFS
class Solution { public List> levelOrder(TreeNode root) { List
> res = new ArrayList<>(); dfs(root, 0, res); return res; } private void dfs(TreeNode root, int level, List
> res) { if (root == null) return; if (level >= res.size()) res.add(new ArrayList
()); //level >= res.size() ---- from 0 >= 0 to initialize res.get(level).add(root.val); dfs(root.left, level+1, res); dfs(root.right, level+1, res); } }
public class Solution { public ArrayList> levelOrderBottom(TreeNode root) { ArrayList > res = new ArrayList<>(); if (root == null) return res; helper(root, res, 0); return res; } public void helper(TreeNode root, ArrayList > res, int index) { int size = res.size(); if (index == size) { ArrayList curList = new ArrayList<>(); curList.add(root.val); res.add(0, curList); } else res.get(size-index-1).add(root.val); if (root.left != null) helper(root.left, res, index+1); if (root.right != null) helper(root.right, res, index+1); } }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/66025.html
Problem Given a binary tree, return the zigzag level order traversal of its nodes values. (ie, from left to right, then right to left for the next level and alternate between). Example Given binary tr...
摘要:做了几道二分法的题目练手,发现这道题已经淡忘了,记录一下。这道题目的要点在于找的区间。边界条件需要注意若或数组为空,返回空当前进到超出末位,或超过,返回空每次创建完根节点之后,要将加,才能进行递归。 Construct Binary Tree from Inorder and Preorder Traversal Problem Given preorder and inorder t...
Problem Flatten a binary tree to a fake linked list in pre-order traversal.Here we use the right pointer in TreeNode as the next pointer in ListNode. Example 1 1 ...
摘要:根据二叉平衡树的定义,我们先写一个求二叉树最大深度的函数。在主函数中,利用比较左右子树的差值来判断当前结点的平衡性,如果不满足则返回。 Problem Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as...
Problem Binary Tree PruningWe are given the head node root of a binary tree, where additionally every nodes value is either a 0 or a 1. Return the same tree where every subtree (of the given tree) not...
阅读 3693·2021-10-12 10:11
阅读 1939·2019-08-30 15:53
阅读 1549·2019-08-30 13:15
阅读 2272·2019-08-30 11:25
阅读 1770·2019-08-29 11:24
阅读 1616·2019-08-26 13:53
阅读 3488·2019-08-26 13:22
阅读 1713·2019-08-26 10:24