Binary Tree Level Order Traversal II

28 篇文章 0 订阅

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).

For example:
Given binary tree {3,9,20,#,#,15,7},

    3
   / \
  9  20
    /  \
   15   7

return its bottom-up level order traversal as:

[
  [15,7]
  [9,20],
  [3],
]

confused what "{1,#,2,3}" means?

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ArrayList<ArrayList<Integer>> levelOrderBottom(TreeNode root) {
        // Start typing your Java solution below
        // DO NOT write main() function
            	ArrayList<Integer> inner_list = new ArrayList<Integer>();
		ArrayList<ArrayList<Integer>> outer_list = new ArrayList<ArrayList<Integer>>();

		if (root == null) {
            return outer_list;
		}

		Queue<TreeNode> current = new LinkedList<TreeNode>();
		Queue<TreeNode> next = new LinkedList<TreeNode>();
		Stack<ArrayList<Integer>> stack = new Stack<ArrayList<Integer>>();
		TreeNode temp;
		current.offer(root);
		while (!current.isEmpty()) {
			temp = current.poll();
			inner_list.add(temp.val);
			if (temp.left != null) {
				next.offer(temp.left);
			}
			if (temp.right != null) {
				next.offer(temp.right);
			}
			if (current.isEmpty()) {
				stack.push(inner_list);
				inner_list = new ArrayList<Integer>();
				// wrong using inner_list.clear()
				// clear() will clear the content in heap
				// elements in outer_list will also get cleaned up
				Queue<TreeNode> temp_queue = current;
				current = next;
				next = temp_queue;
			}
		}
		while(!stack.isEmpty()){
			outer_list.add(stack.pop());
		}
		return outer_list;
        
    }
}

Cannot use peek() on an empty Stack, it would give out an exception. Instead, use isEmpty().


Solution 2. Using DFS level order traversal

	public ArrayList<ArrayList<Integer>> levelOrder2(TreeNode root) {
		// Start typing your Java solution below
		// DO NOT write main() function
		ArrayList<Integer> inner_list = new ArrayList<Integer>();
		ArrayList<ArrayList<Integer>> outer_list = new ArrayList<ArrayList<Integer>>();
		int height = getHeight(root);
		while (height > 0) {
			levelTraversal(inner_list, root, height);
			outer_list.add(new ArrayList<Integer>(inner_list));
			inner_list.clear();
			height--;
		}
		return outer_list;
	}

	public void levelTraversal(ArrayList<Integer> inner_list, TreeNode node,
			int level) {
		if (node == null)
			return;
		if (level == 1)
			inner_list.add(node.val);
		else {
			levelTraversal(inner_list, node.left, level - 1);
			levelTraversal(inner_list, node.right, level - 1);
		}
	}

	public int getHeight(TreeNode root) {
		if (root == null)
			return 0;
		int left = getHeight(root.left);
		int right = getHeight(root.right);
		return left > right ? left + 1 : right + 1;
	}


http://leetcode.com/2010/09/binary-tree-level-order-traversal-using_17.html

Answer:
Although the DFS solution traverse the same node multiple times, it is not another order slower than the BFS solution. Here is the proof that the DFS solution above runs in O(N) time, where N is the number of nodes in the binary tree and we assume that the binary tree is balanced.

We first compute the complexity of printLevel for the kth level:

T(k) = 2T(k-1) + c
     = 2k-1 T(1) + c
     = 2k-1 + c

Assuming it’s a balanced binary tree, then it would have a total of lg N levels.

Therefore, the complexity of printing all levels is:

T(1) + T(2) + ... + T(lg N)
= 1 + 2 + 22 + ... + 2lg N-1 + c
= O(N)

Finding the maximum height of the tree also takes O(N) time, therefore the overall complexity is still O(N).


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值