Leetcode bfs&dfs Binary Tree Postorder Traversal

Binary Tree Level Order Traversal

Total Accepted: 20571 Total
Submissions: 66679My Submissions

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,记录指针向节点的指针和每个节点所在的层

思路2:dfs

递归函数:

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

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

复杂度:

bfs 时间O(n) ,空间O(n)

dfs 时间O(n) ,空间O(log n)

/**
 * 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;
    }
};

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;
    }
};

时间: 2024-10-07 15:25:07

Leetcode bfs&dfs Binary Tree Postorder Traversal的相关文章

Leetcode bfs&amp;dfs Binary Tree Postorder Traversal II

Binary Tree Level Order Traversal II Total Accepted: 16983 Total Submissions: 54229My Submissions 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 ex

LeetCode解题报告:Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 注意:下面是迭代的解法.理解有点困难,和大家讨论一下. 1 import java.uti

Leetcode dfs Binary Tree Postorder Traversal

Binary Tree Postorder Traversal Total Accepted: 28560 Total Submissions: 92333My Submissions Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive s

【leetcode】145. Binary Tree Postorder Traversal

题目如下: 解题思路:凑数题+3,搞不懂为什么本题的难度是Hard,而[leetcode]590. N-ary Tree Postorder Traversal是Medium. 代码如下: # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solutio

【LeetCode】145. Binary Tree Postorder Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51494797 Subject 出处:https://leetcode.com/problems/binary-tree-postorder-traversal/ Hard 级别 Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree

leetcode || 145、Binary Tree Postorder Traversal

problem: Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? Hide Tags Tree Stack 题意:非递归后续遍历二叉树

Leetcode dfs Binary Tree Postorder Traversal II

Sudoku Solver Total Accepted: 11752 Total Submissions: 56537My Submissions Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution

&lt;LeetCode OJ&gt; 145. Binary Tree Postorder Traversal

在此之前回顾前序遍历和中序遍历: 1,前序遍历: 基本规则,总是先访问根节点在左节点,在右节点 递归解法: class Solution { public: vector<int> result; vector<int> preorderTraversal(TreeNode* root) { if(root){ result.push_back(root->val); preorderTraversal(root->left); preorderTraversal(ro

LeetCode: Binary Tree Postorder Traversal 解题报告

Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3},   1    \     2    /   3return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively