zoukankan      html  css  js  c++  java
  • LeetCode: Binary Tree Level Order Traversal II

    跟1基本一样,就是用个stack就解决了,算一次过吧

     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     void dfs(queue<TreeNode *> &pre, stack<vector<int>> &save) {
    13         queue<TreeNode *> S;
    14         vector<int> level;
    15         while (!pre.empty()) {
    16             TreeNode *tmp = pre.front();
    17             if (tmp->left) S.push(tmp->left);
    18             if (tmp->right) S.push(tmp->right);
    19             level.push_back(tmp->val);
    20             pre.pop();
    21         }
    22         save.push(level);
    23         if (!S.empty()) {
    24             pre = S;
    25             dfs(pre, save);
    26         }
    27     }
    28     vector<vector<int> > levelOrderBottom(TreeNode *root) {
    29         // Start typing your C/C++ solution below
    30         // DO NOT write int main() function
    31         stack<vector<int>> save;
    32         queue<TreeNode *> pre;
    33         vector<vector<int>> ret;
    34         if (!root) return ret;
    35         pre.push(root);
    36         dfs(pre, save);
    37         while (!save.empty()) {
    38             vector<int> tmp = save.top();
    39             ret.push_back(tmp);
    40             save.pop();
    41         }
    42         return ret;
    43     }
    44 };

     后来写了个更好的代码

     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> > levelOrderBottom(TreeNode *root) {
    13         // Start typing your C/C++ solution below
    14         // DO NOT write int main() function
    15         vector<vector<int> > ret;
    16         if (!root) return ret;
    17         vector<int> cur;
    18         queue<TreeNode *> S, T;
    19         S.push(root);
    20         while (!S.empty()) {
    21             cur.clear();
    22             while (!S.empty()) {
    23                 TreeNode *tmp = S.front();
    24                 S.pop();
    25                 cur.push_back(tmp->val);
    26                 if (tmp->left) T.push(tmp->left);
    27                 if (tmp->right) T.push(tmp->right);
    28             }
    29             ret.push_back(cur);
    30             S.swap(T);
    31         }
    32         reverse(ret.begin(), ret.end());
    33         return ret;
    34     }
    35 };

     再加个recursive的方法

     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 private:
    12     vector<vector<int> > res;
    13 public:
    14     void dfs(TreeNode *root, int d) {
    15         if (!root) return;
    16         if (d >= res.size()) res.push_back(vector<int>(0));
    17         res[d].push_back(root->val);
    18         dfs(root->left, d+1);
    19         dfs(root->right, d+1);
    20     }
    21     vector<vector<int> > levelOrderBottom(TreeNode *root) {
    22         // IMPORTANT: Please reset any member data you declared, as
    23         // the same Solution instance will be reused for each test case.
    24         res.clear();
    25         dfs(root, 0);
    26         for (int i = 0; i < res.size()/2; i++) swap(res[i], res[res.size()-1-i]);
    27         return res;
    28     }
    29 };

     C#:

     1 /**
     2  * Definition for a binary tree node.
     3  * public class TreeNode {
     4  *     public int val;
     5  *     public TreeNode left;
     6  *     public TreeNode right;
     7  *     public TreeNode(int x) { val = x; }
     8  * }
     9  */
    10 public class Solution {
    11     public List<List<int>> LevelOrderBottom(TreeNode root) {
    12         List<List<int>> ans = new List<List<int>>();
    13         if (root == null) return ans;
    14         List<int> tmp = new List<int>();
    15         Queue<TreeNode> que = new Queue<TreeNode>();
    16         que.Enqueue(root);
    17         que.Enqueue(null);
    18         while (true)
    19         {
    20             TreeNode peek = que.Peek();
    21             que.Dequeue();
    22             if (peek == null)
    23             {
    24                 ans.Add(tmp);
    25                 if (que.Count == 0 || que.Peek() == null) break;
    26                 tmp = new List<int>();
    27                 que.Enqueue(null);
    28             }
    29             else {
    30                 if (peek.left != null) que.Enqueue(peek.left);
    31                 if (peek.right != null) que.Enqueue(peek.right);
    32                 tmp.Add(peek.val);
    33             }
    34         }
    35         ans.Reverse();
    36         return ans;
    37     }
    38 }
    View Code
  • 相关阅读:
    如何在Web项目中给没有添加API核心组件添加APIController的帮助页HelpPage
    如何在MVC_WebAPI项目中的APIController帮助页面添加Web测试工具测试
    exception throw in progress runner thread_VS2015中SVN源代码无说明提交异常
    [转]AMBA、AHB、APB、ASB总线简介
    UML和模式应用4:初始阶段(6)--迭代方法中如何使用用例
    UML和模式应用4:初始阶段(5)--用例编写的准则
    startup_MK64F12.s文件解析
    [转] bss段、data段、text段
    [转]GDB-----2.watchpoint
    [转]GDB-----1.GDB概述
  • 原文地址:https://www.cnblogs.com/yingzhongwen/p/2965503.html
Copyright © 2011-2022 走看看