leetcode题解:Tree Level Order Traversal II (二叉树的层序遍历 2)

题目:

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.

说明:

        1)与层序遍历1相似,只是本题遍历方向不同:从下往上,其他相同

2) 代码只要加上一行逆序输出的语句即可

实现:

一、 递归实现:

 1 *
 2  * Definition for binary tree
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11     public:
12     vector<vector<int> > levelOrder(TreeNode *root) {
13         vector<vector<int>> result;
14         traverse(root,1,result);
15        std::reverse(result.begin(),result.end());//比层序遍历1多此一行
16         return result;
17     }
18     void traverse(TreeNode *root,size_t level,vector<vector<int>> &result)
19     {
20         if(root==nullptr) return;
21         if(level>result.size()) result.push_back(vector<int>());
22         result[level-1].push_back(root->val);
23         traverse(root->left,level+1,result);
24         traverse(root->right,level+1,result);
25     }
26 };

二、迭代实现:

 1 /**
 2  * Definition for binary tree
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10  /*常规层序遍历思想,每层入队结束压入一个空节点,作为标志*/
11 class Solution {
12 public:
13     vector<vector<int> > levelOrder(TreeNode *root) {
14         vector<vector<int>> vec_vec_tree;//创建空vector,存放最后返回的遍历二叉树的值
15         vector<int> level;//创建空的vector,存放每一层的遍历二叉树的值
16         TreeNode *p=root;
17         if(p==nullptr) return vec_vec_tree;//如果二叉树空,返回空vector<vector<int>>
18         queue<TreeNode *> queue_tree;//创建一个空队列
19         queue_tree.push(p);//root节点入队列
20         queue_tree.push(nullptr);//空节点入队列
21         while(!queue_tree.empty())//直到队列为空
22         {
23             p=queue_tree.front();    //头结点取值,并出队列
24             queue_tree.pop();
25             if(p==nullptr&&!level.empty())//节点为空并且队列不为空
26             {
27                 queue_tree.push(nullptr);//入队空节点,与下一层隔开
28                 vec_vec_tree.push_back(level);//已遍历的层入队
29                 level.clear();//清空vecor level
30             }
31             else if(p!=nullptr)//如果节点不空
32             {
33             level.push_back(p->val);//遍历
34             if(p->left) queue_tree.push(p->left);//若有左右孩子,则入队列
35             if(p->right) queue_tree.push(p->right);//注意入队顺序:先左后右
36             }
37
38         }
39         std::reverse(vec_vec_tree.begin(),vec_vec_tree.end());//比层序遍历1多此一行
40         return vec_vec_tree;
41     }
42 };

b 迭代实现2

 1 /**
 2  * Definition for binary tree
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10  /*两个队列实现*/
11 class Solution {
12 public:
13     vector<vector<int> > levelOrder(TreeNode *root) {
14         vector<vector<int> > result;
15         if(root == nullptr) return result;
16          queue<TreeNode*> current, next;//两个队列,保存当层(current)和下层(next)节点
17         vector<int> level; // 存放每层的节点值
18         current.push(root);
19         while (!current.empty())//直到二叉树遍历完成
20         {
21             while (!current.empty())//直到本层二叉树遍历完成
22             {
23                 TreeNode* node = current.front();//取队首节点,出队列
24                 current.pop();
25                 level.push_back(node->val);//访问节点值
26                 if (node->left != nullptr) next.push(node->left);//若存在左右节点,则压入next队列中
27                 if (node->right != nullptr) next.push(node->right);//注意入队顺序为先left后right
28             }
29             result.push_back(level);//压入总vector
30             level.clear();//清vector
31             swap(next, current);//next队列和current队列交换
32         }
33         std::reverse(result.begin(),result.end());//比层序遍历1多此一行
34         return result;
35         }
36     };

leetcode题解:Tree Level Order Traversal II (二叉树的层序遍历 2)

时间: 2024-11-08 19:25:11

leetcode题解:Tree Level Order Traversal II (二叉树的层序遍历 2)的相关文章

LeetCode Binary Tree Level Order Traversal II (二叉树颠倒层序)

题意:从左到右统计将同一层的值放在同一个容器vector中,要求上下颠倒,左右不颠倒. 思路:广搜逐层添加进来,最后再反转. 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9

Leetcode 102. Binary Tree Level Order Traversal(二叉树的层序遍历)

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,null,null,15,7], 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ]

LeetCode OJ:Binary Tree Level Order Traversal(二叉树的层序遍历)

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] ] 层序遍历,既

lintcode 中等题:binary tree level order traversal ii 二叉树的层次遍历II

题目 二叉树的层次遍历 II 给出一棵二叉树,返回其节点值从底向上的层次序遍历(按从叶节点所在层到根节点所在的层遍历,然后逐层从左往右遍历) 样例 给出一棵二叉树 {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 按照从下往上的层次遍历为: [ [15,7], [9,20], [3] ] 解题 和上一题的答案反过来 直接每次在list中第一个位置加入当前层结点 /** * Definition of TreeNode: * public class TreeNode { *

107 Binary Tree Level Order Traversal II 二叉树的层次遍历 II

给定一个二叉树,返回其节点值自底向上的层次遍历. (即按从叶节点所在层到根节点所在的层,逐层从左向右遍历)例如:给定二叉树 [3,9,20,null,null,15,7],    3   / \  9  20    /  \   15   7返回其自自底向上的层次遍历为:[  [15,7],  [9,20],  [3]]详见:https://leetcode.com/problems/binary-tree-level-order-traversal-ii/description/ /** *

LeetCode——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 trave

LeetCode: Binary Tree Level Order Traversal II [107]

[题目] 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

[leetcode]Binary Tree Level Order Traversal II @ Python

原题地址:http://oj.leetcode.com/problems/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

leetCode 107. Binary Tree Level Order Traversal II 二叉树层次遍历反转

107. 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,null,null,15,7],     3  

[LeetCode]Binary Tree Level Order Traversal II

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 re