博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【LeetCode】107. Binary Tree Level Order Traversal II (2 solutions)
阅读量:5117 次
发布时间:2019-06-13

本文共 2917 字,大约阅读时间需要 9 分钟。

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? 

OJ's Binary Tree Serialization:

The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.

Here's an example:

1  / \ 2   3    /   4    \     5
The above binary tree is serialized as 
"{1,2,3,#,#,4,#,#,5}".
 
解法一:递归
参考了Discussion中stellari的做法,递归进行层次遍历,并将每个level对应于相应的vector。
/** * Definition for binary tree * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution{public:    vector
> result; void levelTra(TreeNode *root, int level) { if(root == NULL) return; if(level == result.size()) { vector
v; result.push_back(v); } result[level].push_back(root->val); levelTra(root->left, level+1); levelTra(root->right, level+1); } vector
> levelOrderBottom(TreeNode *root) { levelTra(root, 0); return vector
>(result.rbegin(), result.rend()); }};

 

解法二:普通层次遍历后逆序。

/** * Definition for binary tree * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ struct Node{    TreeNode* tNode;    int level;    Node(TreeNode* newtNode, int newlevel): tNode(newtNode), level(newlevel) {}};class Solution {public:    vector
> levelOrderBottom(TreeNode *root) { vector
> ret; if(!root) return ret; // push root Node* rootNode = new Node(root, 0); queue
Nqueue; Nqueue.push(rootNode); vector
cur; int curlevel = 0; while(!Nqueue.empty()) { Node* frontNode = Nqueue.front(); Nqueue.pop(); if(frontNode->level > curlevel) { ret.push_back(cur); cur.clear(); curlevel = frontNode->level; } cur.push_back(frontNode->tNode->val); if(frontNode->tNode->left) { Node* leftNode = new Node(frontNode->tNode->left, frontNode->level+1); Nqueue.push(leftNode); } if(frontNode->tNode->right) { Node* rightNode = new Node(frontNode->tNode->right, frontNode->level+1); Nqueue.push(rightNode); } } ret.push_back(cur); reverse(ret.begin(), ret.end()); return ret; }};

转载于:https://www.cnblogs.com/ganganloveu/p/3843470.html

你可能感兴趣的文章
ubuntu无法解析主机错误与解决的方法
查看>>
尚学堂Java面试题整理
查看>>
MySQL表的四种分区类型
查看>>
[BZOJ 3489] A simple rmq problem 【可持久化树套树】
查看>>
STM32单片机使用注意事项
查看>>
swing入门教程
查看>>
好莱坞十大导演排名及其代表作,你看过多少?
查看>>
Loj #139
查看>>
hihocoder1187 Divisors
查看>>
Azure 托管镜像和非托管镜像对比
查看>>
js window.open 参数设置
查看>>
032. asp.netWeb用户控件之一初识用户控件并为其自定义属性
查看>>
Ubuntu下安装MySQL及简单操作
查看>>
前端监控
查看>>
clipboard.js使用方法
查看>>
移动开发平台-应用之星app制作教程
查看>>
leetcode 459. 重复的子字符串(Repeated Substring Pattern)
查看>>
伪类与超链接
查看>>
centos 7 redis-4.0.11 主从
查看>>
博弈论 从懵逼到入门 详解
查看>>