Leetcode - Tree - Binary Tree Traversal

1、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?

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> preorderTraversal(TreeNode *root) {
        vector<int> nodeValue;
        preorder(root, nodeValue);
        return nodeValue;
    }
    void preorder(TreeNode *root, vector<int> &nodeValue)
    {
        if(root==NULL)
            return;
        nodeValue.push_back(root->val);
        preorder(root->left, nodeValue);
        preorder(root->right, nodeValue);
    }
};
/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> preorderTraversal(TreeNode *root) {
        // 时间复杂度为O(n),空间复杂度为O(h)
        vector<int> nodeValue;
        if(root==NULL)
            return nodeValue;
        stack<TreeNode*> nodeStack;
        nodeStack.push(root);
        while(!nodeStack.empty())
        {
            root=nodeStack.top();
            nodeStack.pop();
            nodeValue.push_back(root->val);
            if(root->right!=NULL)
                nodeStack.push(root->right);
            if(root->left!=NULL)
                nodeStack.push(root->left);
        }
        return nodeValue;
    }
};


2、Binary Tree Inorder Traversal

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?

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> inorderTraversal(TreeNode *root) {
        vector<int> nodeValue;
        inorder(root, nodeValue);
        return nodeValue;
    }
    void inorder(TreeNode *root, vector<int> &nodeValue)
    {
        if(root==NULL)
            return;
        inorder(root->left,nodeValue);
        nodeValue.push_back(root->val);
        inorder(root->right,nodeValue);
    }
};
/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> inorderTraversal(TreeNode *root) {
        vector<int> nodeValue;
        stack<TreeNode*> nodeStack;
        while(root!=NULL||!nodeStack.empty())
        {
            //运行到这儿,root不允许为之前出现过的结点,否则死循环
            while(root!=NULL)
            {
                nodeStack.push(root);
                root=root->left;
            }
            if(!nodeStack.empty())
            {
                root=nodeStack.top();
                nodeValue.push_back(root->val);
                nodeStack.pop();
                root=root->right;
            }
        }
        return nodeValue;
    }
};

3、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 you do it iteratively?

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> postorderTraversal(TreeNode *root) {
        vector<int> nodeValue;
        postorder(root, nodeValue);
        return nodeValue;
    }
    void postorder(TreeNode *root, vector<int> &nodeValue)
    {
        if(root==NULL)
            return;
        postorder(root->left, nodeValue);
        postorder(root->right, nodeValue);
        nodeValue.push_back(root->val);
    }
};

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> postorderTraversal(TreeNode *root) {
        vector<int> nodeValue;
        stack<TreeNode*> nodeStack;
        if(root!=NULL)
        {
            nodeStack.push(root);
            while(!nodeStack.empty())
            {
                root=nodeStack.top();
                nodeStack.pop();
                nodeValue.push_back(root->val);
                if(root->left!=NULL) nodeStack.push(root->left);
                if(root->right!=NULL) nodeStack.push(root->right);
            }
            reverse(nodeValue.begin(), nodeValue.end());
        }
        return nodeValue;
    }
};

4、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]
]
/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<vector<int> > levelOrder(TreeNode *root) {
        vector<vector<int> > path;
        vector<int> levelPath;
        if(root==NULL)
            return path;
        queue<TreeNode*> node_handling, node_waiting;
        node_waiting.push(root);
        while(!node_waiting.empty())
        {
            swap(node_handling,node_waiting);
            while(!node_handling.empty())
            {
                root=node_handling.front();
                node_handling.pop();
                levelPath.push_back(root->val);
                if(root->left!=NULL)node_waiting.push(root->left);
                if(root->right!=NULL)node_waiting.push(root->right);
            }
            path.push_back(levelPath);
            levelPath.clear();
        }
        return path;
    }
};


5、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]
]
/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<vector<int> > levelOrderBottom(TreeNode *root) {
        vector<vector<int>> path;
        int depth=0;
        nLevelOrder(root, depth, path);
        reverse(path.begin(), path.end());
        return path;
    }
    void nLevelOrder(TreeNode *root, int depth, vector<vector<int>> &path)
    {
        if(root!=NULL)
        {
            if(depth>=path.size())
            {
                vector<int> temp{root->val};
                path.push_back(temp);
            }
            else
                path[depth].push_back(root->val);
            nLevelOrder(root->left, depth+1, path);
            nLevelOrder(root->right, depth+1, path);
        }
    }
};
/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<vector<int> > levelOrderBottom(TreeNode *root) {
        vector<vector<int>> path;
        vector<int> levelPath;
        if(root!=NULL)
        {
            queue<TreeNode*> node_handling, node_waiting;
            node_waiting.push(root);
            while(!node_waiting.empty())
            {
                swap(node_handling,node_waiting);
                while(!node_handling.empty())
                {
                    root=node_handling.front();
                    node_handling.pop();
                    levelPath.push_back(root->val);
                    if(root->left) node_waiting.push(root->left);
                    if(root->right) node_waiting.push(root->right);
                }
                
                path.push_back(levelPath);
                levelPath.clear();
            }
            reverse(path.begin(),path.end());
        }
        return path;
    }
};


6、Binary Tree Zigzag Level Order Traversal

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).

For example:
Given binary tree {3,9,20,#,#,15,7},

    3
   / \
  9  20
    /  \
   15   7

return its zigzag level order traversal as:

[
  [3],
  [20,9],
  [15,7]
]
/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<vector<int> > zigzagLevelOrder(TreeNode *root) {
        vector<vector<int>> path;
        vector<int> levelPath;
        if(root!=NULL)
        {
            bool zigzag=false;
            queue<TreeNode*> node_handling, node_waiting;
            node_waiting.push(root);
            while(!node_waiting.empty())
            {
                swap(node_handling, node_waiting);
                while(!node_handling.empty())
                {
                    root=node_handling.front();
                    node_handling.pop();
                    levelPath.push_back(root->val);
                    if(root->left)node_waiting.push(root->left);
                    if(root->right)node_waiting.push(root->right);
                }
                if(zigzag)
                {
                    reverse(levelPath.begin(), levelPath.end());
                    zigzag=false;
                }
                else
                    zigzag=true;
                path.push_back(levelPath);
                levelPath.clear();
            }
        }
        return path;
    }
};






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值