zoukankan      html  css  js  c++  java
  • [Leetcode 94] 72 Binary Tree Inorder Traversal

    Problem:

    Given a binary tree, return the inorder traversal of its nodes' values.

    For example:
    Given binary tree {1,#,2,3},

       1
        
         2
        /
       3
    

    return [1,3,2].

    Note: Recursive solution is trivial, could you do it iteratively?

    Analysis:

    Basic tree traversal problems. Recursive solution is just to proper arrange the visiting order of the tree. Iterative version need the help of stack. My own solution is that everytime pop a node from the stack, if it has no children nodes, then record it in the result vector. Else we must first push it right child node,  then itself and at last its left child node. If any of the node is NULL, skip the pushing operation. Another thing to do is set the node's left and right pointer to NULL.

    Code:

    Recursive Version:

     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<int> res;
    13 
    14     vector<int> inorderTraversal(TreeNode *root) {
    15         // Start typing your C/C++ solution below
    16         // DO NOT write int main() function
    17         res.clear();
    18         
    19         inorder(root);
    20         
    21         return res;
    22     }
    23     
    24     
    25 private:
    26     void inorder(TreeNode *node) {
    27         if (node == NULL)
    28             return ;
    29             
    30         inorder(node->left);
    31         res.push_back(node->val);
    32         inorder(node->right);
    33     }
    34 };
    View Code

    Iterative Version:

     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<int> inorderTraversal(TreeNode *root) {
    13         // Start typing your C/C++ solution below
    14         // DO NOT write int main() function
    15         vector<int> res;
    16         
    17         if (root == NULL)
    18             return res;
    19         
    20         stack<TreeNode *> stk;
    21         TreeNode *tmp, *node;
    22         stk.push(root);
    23         do {
    24             node = stk.top();
    25             stk.pop();
    26             
    27             if (node->left == NULL && node->right == NULL) {
    28                 res.push_back(node->val);
    29                 continue;
    30             }
    31             
    32             if (node->right != NULL) {
    33                 stk.push(node->right);
    34                 node->right = NULL;
    35             }
    36             
    37             if (node->left != NULL) {
    38                 tmp = node->left;
    39                 node->left = NULL;
    40                 stk.push(node);
    41                 stk.push(tmp);
    42             } else 
    43                 stk.push(node);
    44             
    45         } while (!stk.empty());
    46         
    47         
    48         return res;
    49     }
    50 };
    View Code
  • 相关阅读:
    go标准库的学习-net/http
    go标准库的学习-sync互斥
    sqlplus命令大全
    Oracle 11g安装步骤详谈
    安装64位版Oracle11gR2后无法启动SQLDeveloper的解决方案(原创) (2016-10-29 下午01:56)
    Dos命令查看端口占用及关闭进程
    access denied ("java.net.SocketPermission" "localhost:1527" "listen,resolve")
    Hibernate的集合映射与sort、order-by属性
    Hibernate总结2 API和配置文件
    MyEclipse8.5启动无法选择工作空间的问题
  • 原文地址:https://www.cnblogs.com/freeneng/p/3220947.html
Copyright © 2011-2022 走看看