Binary Tree Level Order Traversal 、Binary Tree Level Order Traversal II

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]

]

解题思路:

求一棵二叉树的各层节点,并且按层存储在二元数组中。利用BFS即可实现。我的博客《二元树的生成、遍历、以及最短最长路径查询》中已经提到,利用队列可实现二元树的横向遍历,这本题中,我们依旧利用队列对树进行BFS,使用两个队列,q表示本层节点,q1表示下层节点,依次进队列,出队列,便可构造二维向量。

代码如下:

class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> res;
        vector<int> temp;
        if(root==NULL) return res;
        queue<TreeNode*> q;
        q.push(root);
        TreeNode *node;
        while(!q.empty())
        {
            queue<TreeNode*> q1;
            while(!q.empty())
            {
                node=q.front();
                temp.push_back(node->val);
                q.pop();
                if(node->left!=NULL)
                {
                    q1.push(node->left);
                }
                if(node->right!=NULL)
                {
                    q1.push(node->right);
                }

            }
            res.push_back(temp);
            q=q1;
            temp.clear();
        }
        return res;
    }
};

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

解题思路:

同Binary Tree Level Order Traversal一样,最后将二元向量翻转即可,代码如下:

class Solution {
public:
    vector<vector<int>> levelOrderBottom(TreeNode* root) {
        vector<vector<int>> res;
        vector<int> temp;
        if(root==NULL) return res;
        queue<TreeNode*> q;
        q.push(root);
        TreeNode* node;
        while(!q.empty())
        {
            queue<TreeNode*> q1;
            while(!q.empty())
            {
                node=q.front();
                q.pop();
                temp.push_back(node->val);
                if(node->left!=NULL)
                q1.push(node->left);
                if(node->right!=NULL)
                q1.push(node->right);
            }
            res.push_back(temp);
            temp.clear();
            q=q1;
        }
        reverse(res.begin(),res.end());
        return res;
    }
};

时间: 2024-08-09 16:28:29

Binary Tree Level Order Traversal 、Binary Tree Level Order Traversal II的相关文章

leetcode || 102、Binary Tree Level Order Traversal

problem: 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

1064. Complete Binary Search Tree (30)【二叉树】——PAT (Advanced Level) Practise

题目信息 1064. Complete Binary Search Tree (30) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less tha

leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal &amp; Construct Binary Tree f

1.  Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 代码: class Solution { public: TreeNode *buildTr

1102. Invert a Binary Tree (25)【二叉树】——PAT (Advanced Level) Practise

题目信息 1102. Invert a Binary Tree (25) 时间限制400 ms 内存限制65536 kB 代码长度限制16000 B The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote (Homebrew), but you can't invert a binary tree on a whiteboard so fuck off.

36. Construct Binary Tree from Inorder and Postorder Traversal &amp;&amp; Construct Binary Tree from Preorder and Inorder Traversal

Construct Binary Tree from Inorder and Postorder Traversal OJ: https://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assu

[leetcode]1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree

[leetcode]1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree 链接 leetcode 描述 ??Given two binary trees original and cloned and given a reference to a node target in the original tree. ??The cloned tree is a copy of the original tr

1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree

Given two binary trees original and cloned and given a reference to a node target in the original tree. The cloned tree is a copy of the original tree. Return a reference to the same node in the cloned tree. Note that you are not allowed to change an

LeetCode 50 Pow(x, n)(Math、Binary Search)(*)

翻译 实现pow(x, n). 原文 Implement pow(x, n). 分析 首先给大家推荐维基百科: zh.wikipedia.org/wiki/二元搜尋樹 en.wikipedia.org/wiki/Binary_search_tree 其次,大家也可以看看类似的一道题: LeetCode 69 Sqrt(x)(Math.Binary Search)(*) 然而这题我还是没有解出来,看看别人的解法-- class Solution { private: double myPowHel

postman中 form-data、x-www-form-urlencoded、raw、binary的区别

1.form-data:  就是http请求中的multipart/form-data,它会将表单的数据处理为一条消息,以标签为单元,用分隔符分开.既可以上传键值对,也可以上传文件.当上传的字段是文件时,会有Content-Type来说明文件类型:content-disposition,用来说明字段的一些信息:由于有boundary隔离,所以multipart/form-data既可以上传文件,也可以上传键值对,它采用了键值对的方式,所以可以上传多个文件. 2.x-www-form-urlenc