[LeetCode13]Binary Tree Level Order Traversal II

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? > read more on how binary tree is serialized on OJ.

Analysis:

Similar with the former Binary Tree Level Order Traversal

Difference is to record the level in reverse order.

Java

public List<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>> result = new ArrayList<>();
		ArrayList<TreeNode> temp = new ArrayList<TreeNode>();
		if(root == null) return result;
		temp.add(root);
		int index = 0;
		int nextLevCount = 1;
		while(index<temp.size()){
			int curLevCount = nextLevCount;
			nextLevCount = 0;
			ArrayList<Integer> level = new ArrayList<Integer>();
			for(int i = index;i<index+curLevCount;i++){
				root = temp.get(i);
				level.add(root.val);
				if(root.left!=null){
					nextLevCount++;
					temp.add(root.left);
				}
				if(root.right!=null){
					nextLevCount++;
					temp.add(root.right);
				}
			}
			result.add(level);
			index+=curLevCount;
		}
		Collections.reverse(result);
		return result;
    }

solution2

public void connect(TreeLinkNode root) {
        // IMPORTANT: Please reset any member data you declared, as
        // the same Solution instance will be reused for each test case.
        if(root == null)
        	return;
        if(root.left != null){
        	TreeLinkNode left = root.left;
        	TreeLinkNode right = root.right;
        	while(left != null){
        		left.next = right;
        		left = left.right;
        		right = right.left;
        	}
        	connect(root.left);
        	connect(root.right);
        }
    }


c++

vector<vector<int> > levelOrderBottom(TreeNode *root) {
        vector<vector<int>> result;
    vector<TreeNode*> sta;
    if(root == NULL) return result;
    sta.push_back(root);
    int nextLevCou = 1;
    int index = 0;
    while(index < sta.size()){
        int curLevCou = nextLevCou;
        nextLevCou = 0;
        vector<int > level;
        for(int i = index; i<index+curLevCou; i++){
            root = sta[i];
            level.push_back(root->val);
            if(root->left != NULL){
                sta.push_back(root->left);
                nextLevCou++;
            }
            if(root->right !=NULL){
                sta.push_back(root->right);
                nextLevCou++;
            }

        }
        result.push_back(level);
        index = index+curLevCou;
    }
    reverse(result.begin(), result.end());
    return result;
    }







评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值