Leetcode 树 Binary Tree Level Order Traversal

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie

Binary Tree Level Order Traversal

Total Accepted: 12292 Total
Submissions: 40417

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]
]

confused what "{1,#,2,3}" means? >
read more on how binary tree is serialized on OJ.

题意:给定一棵二叉树,返回按层遍历的结果

思路1:bfs,定义一个新的struct,记录指针向节点的指针和每个节点所在的层

复杂度1:时间O(n),空间O(n)

思路2:dfs

递归函数:

void levelOrder(TreeNode *root, int level, vector<vector<int> >&result)

表示把根为root的树按层存放在result中,其中level表示当前的层数

复杂度2:时间O(n),空间O(n)

相关题目:

Binary Tree Level Order Traversal II

Binary Tree Zigzag Level Order Traversal

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    struct NodeWithLevel{
    	TreeNode *p;
    	int level;
    	NodeWithLevel(TreeNode *pp, int l):p(pp), level(l){}
    };

    //思路1
     vector<vector<int> > levelOrder(TreeNode *root){
    	vector<vector<int> > result;
    	queue<NodeWithLevel > q;
    	q.push(NodeWithLevel(root, 0));
    	while(!q.empty()){
    		NodeWithLevel cur = q.front();q.pop();
    		TreeNode *t = cur.p;
    		if(t != NULL){

    			if(result.size() <= cur.level) {
    				vector<int> v;
    				v.push_back(t->val);
    				result.push_back(v);
    			}else result[cur.level].push_back(t->val);

    			q.push(NodeWithLevel(t->left, cur.level + 1));
    			q.push(NodeWithLevel(t->right, cur.level + 1));
    		}
    	}
    	return result;
    }
};
/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    struct NodeWithLevel{
    	TreeNode *p;
    	int level;
    	NodeWithLevel(TreeNode *pp, int l):p(pp), level(l){}
    };

    //思路2
    void levelOrder(TreeNode *root, int level, vector<vector<int> >&result)
    {
    	if(!root) return ;
    	if(result.size() <= level) {
    		vector<int> v;
    		v.push_back(root->val);
    		result.push_back(v);
    	}else result[level].push_back(root->val);

    	levelOrder(root->left, level + 1, result);
    	levelOrder(root->right, level + 1, result);
    }

    vector<vector<int> > levelOrder(TreeNode *root){
    	vector<vector<int> >result;
    	levelOrder(root, 0, result);
    	return result;
    }
};

Leetcode 树 Binary Tree Level Order Traversal

时间: 2024-10-24 19:53:44

Leetcode 树 Binary Tree Level Order Traversal的相关文章

Leetcode 树 Binary Tree Level Order Traversal II

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Binary Tree Level Order Traversal II Total Accepted: 10080 Total Submissions: 32610 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, l

LeetCode 102 Binary Tree Level Order Traversal(二叉树的层级顺序遍历)(*)

翻译 给定一个二叉树,返回按层级顺序遍历的每个节点的值. 从左到右,逐层遍历. 例如: 给定一个二叉树 {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 返回它的层级顺序遍历结果为: [ [3], [9,20], [15,7] ] 翻译 Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For

LeetCode OJ - Binary Tree Level Order Traversal 1 &amp;&amp; 2

BFS以及它的扩展,我发现栈是个很好用的数据结构,特别是对于顺序需要颠倒的时候!!! 这里有个重要的信息:可以用null来标识一个level的结束!!! 下面是AC代码: 1 /** 2 * Given a binary tree, return the bottom-up level order traversal of its nodes' values. 3 * (ie, from left to right, level by level from leaf to root). 4 *

Java for LeetCode 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,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order trave

【Leetcode】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] ] 思路:使用

【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 (二叉树的层序遍历)

题目: 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] ] co

[LeetCode][JavaScript]Binary Tree Level Order Traversal

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

LeetCode(32)-Binary Tree Level Order Traversal

题目: LeetCode Premium Subscription Problems Pick One Mock Articles Discuss Book fengsehng 102. Binary Tree Level Order Traversal My Submissions QuestionEditorial Solution Total Accepted: 98313 Total Submissions: 302608 Difficulty: Easy Given a binary