Lintcode---二叉树的前序、中序、后序遍历

给出一棵二叉树,返回其节点值的后序遍历。

样例

给出一棵二叉树 {1,#,2,3},

   1
    \
     2
    /
   3

返回 [3,2,1]

 

思路:二叉树的后序遍历,简单题目,递归方法,要很快撸出来,非递归方法也要熟悉;
           后序遍历:左->右->根
   

           注意,注意,注意! 递归的时候一定要注意定义的变量是否会在每次递归时重新定义;

 

/**
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this->val = val;
 *         this->left = this->right = NULL;
 *     }
 * }
 */
class Solution {
    /**
     * @param root: The root of binary tree.
     * @return: Postorder in vector which contains node values.
     */
    /*
    二叉树的后序遍历,简单题目,递归方法,要很快撸出来,非递归方法也要熟悉;
    后序遍历:左->右->根
    注意,注意,注意! 递归的时候一定要注意定义的变量是否会在每次递归时重新定义;
    */
public:

    vector<int> vec;
    vector<int> postorderTraversal(TreeNode *root) {
        // write your code here
        if(root==NULL){
            return vec;
        }
        if(root->left!=NULL){
            postorderTraversal(root->left);
        }
        
        if(root->right!=NULL){
            postorderTraversal(root->right);
        }
        
        vec.push_back(root->val);
        
        return vec;
        
    }
};

 


给出一棵二叉树,返回其中序遍历

样例

给出二叉树 {1,#,2,3},

   1
    \
     2
    /
   3

返回 [1,3,2].

 

思路: 中序遍历:左->根->右
      

           非递归方法,也要熟悉;

 

/**
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this->val = val;
 *         this->left = this->right = NULL;
 *     }
 * }
 */
class Solution {
    /**
     * @param root: The root of binary tree.
     * @return: Inorder in vector which contains node values.
     */
    /*
    用递归的方法,二叉树中序遍历,简单题目;
    中序遍历:左->根->右
    非递归方法,也要熟悉;
    */
public:
    vector<int> vec;
    vector<int> inorderTraversal(TreeNode *root) {
        // write your code here
        
        if(root==NULL){
            return vec;
        }
        
        if(root->left!=NULL){
            inorderTraversal(root->left);
        }
        vec.push_back(root->val);
        
        if(root->right!=NULL){
            inorderTraversal(root->right);
        }
        
        return vec;
    }
};

 


给出一棵二叉树,返回其节点值的前序遍历。

样例

给出一棵二叉树 {1,#,2,3},

   1
    \
     2
    /
   3

 返回 [1,2,3].

 

思路:简单题,递归方法要在五分钟内快速正确的撸出来;
   

          先序遍历:根->左->右

 

/**
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this->val = val;
 *         this->left = this->right = NULL;
 *     }
 * }
 */

class Solution {
public:
    /**
     * @param root: The root of binary tree.
     * @return: Preorder in vector which contains node values.
     */
    /*
    简单题,递归方法要在五分钟内快速正确的撸出来;
    先序遍历:根->左->右
    */
    
    vector<int> vec;
    vector<int> preorderTraversal(TreeNode *root) {
        // write your code here
        if(root==NULL){
            return vec;
        }
        
        vec.push_back(root->val);
        
        if(root->left!=NULL){
            preorderTraversal(root->left);
        }
        
        if(root->right!=NULL){
            preorderTraversal(root->right);
        }
        
        return vec;
    }
};

 

posted @ 2017-07-03 14:23  静悟生慧  阅读(4551)  评论(0编辑  收藏  举报