LeetCode之“树”: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]
]

  这道题利用宽度优先搜索就可以了,具体程序(8ms)如下:

 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  */
10 class Solution {
11 public:
12     vector<vector<int>> levelOrder(TreeNode* root) {
13         vector<vector<int>> retVec;
14         if(!root)
15             return retVec;
16
17         retVec.push_back(vector<int>{root->val});
18         queue<TreeNode *> que;
19         que.push(root);
20         while(true)
21         {
22             vector<int> vec;
23             queue<TreeNode *> q;
24             while(!que.empty())
25             {
26                 TreeNode *tree = que.front();
27                 que.pop();
28                 if(tree->left)
29                 {
30                     vec.push_back((tree->left)->val);
31                     q.push(tree->left);
32                 }
33                 if(tree->right)
34                 {
35                     vec.push_back((tree->right)->val);
36                     q.push(tree->right);
37                 }
38             }
39
40             if(!q.empty())
41             {
42                 que = q;
43                 retVec.push_back(vec);
44             }
45             else
46                 break;
47         }
48
49         return retVec;
50     }
51 };

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

  这道题基本更上边的题目一样,我们只需要将上题的结果retVec最后再反转一下就可以了,即添加如下一行即可:

1 reverse(retVec.begin(), retVec.end());

  这样的程序只要8ms,但下边基本一样的程序却要64ms:

 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  */
10 class Solution {
11 public:
12     vector<vector<int>> levelOrderBottom(TreeNode* root) {
13         vector<vector<int>> retVec;
14         if(!root)
15             return retVec;
16
17         retVec.insert(retVec.begin(), vector<int>{root->val});
18         queue<TreeNode *> que;
19         que.push(root);
20         while(true)
21         {
22             vector<int> vec;
23             queue<TreeNode *> q;
24             while(!que.empty())
25             {
26                 TreeNode *tree = que.front();
27                 que.pop();
28                 if(tree->left)
29                 {
30                     vec.push_back((tree->left)->val);
31                     q.push(tree->left);
32                 }
33                 if(tree->right)
34                 {
35                     vec.push_back((tree->right)->val);
36                     q.push(tree->right);
37                 }
38             }
39
40             if(!q.empty())
41             {
42                 que = q;
43                 retVec.insert(retVec.begin(), vec);
44             }
45             else
46                 break;
47         }
48
49         return retVec;
50     }
51 };

时间: 2024-12-13 09:11:02

LeetCode之“树”:Binary Tree Level Order Traversal && Binary Tree Level Order Traversal II的相关文章

LeetCode: Binary Tree Level Order Traversal &amp;&amp; Binary Tree Zigzag Level Order Traversal

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

35. Binary Tree Level Order Traversal &amp;&amp; Binary Tree Level Order Traversal II

Binary Tree Level Order Traversal OJ: https://oj.leetcode.com/problems/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

LeetCode之“树”:Binary Tree Preorder Traversal &amp;&amp; Binary Tree Inorder Traversal &amp;&amp; Binary Tree Postorder Traversal

Binary Tree Preorder Traversal 题目链接 题目要求: Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 递归解

leetcode - Binary Tree Preorder Traversal &amp;&amp; Binary Tree Inorder Traversal &amp;&amp; Binary Tree Postorder Traversal

简单来说,就是二叉树的前序.中序.后序遍历,包括了递归和非递归的方法 前序遍历(注释中的为递归版本): 1 #include <vector> 2 #include <stack> 3 #include <stddef.h> 4 #include <iostream> 5 6 using namespace std; 7 8 struct TreeNode 9 { 10 int val; 11 TreeNode *left; 12 TreeNode *rig

【LeetCode】105 &amp; 106 Construct Binary Tree from (Preorder and Inorder) || (Inorder and Postorder)Traversal

Description: Given arrays recording 'Preorder and Inorder' Traversal (Problem 105) or  'Inorder and Postorder' (Problem 106), u need build the binary tree. Input: 105. Preorder & Inorder traversal 106. Inorder & Postorder traversal output: A binar

PAT Advanced Level 1064 Complete Binary Search Tree (30)(30 分)

1064 Complete Binary Search Tree (30)(30 分) 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 than the node's key. The right subtree of

【leetcode刷题笔记】Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 题解:递归,树的高度 = max(左子树高度,右子树高度)+1: 代码如下: 1 /** 2 * Definition for binary tree 3 * public cla

Binary Tree Postorder Traversal &amp;&amp; Binary Tree Preorder Traversal

详见:剑指 Offer 题目汇总索引:第6题 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 y

[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

[Leetcode] Binary tree-- 606. Construct String from Binary Tree

You need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way. The null node needs to be represented by empty parenthesis pair "()". And you need to omit all the empty parenthesis pairs t