1. 二叉树的前序遍历、中序遍历和后序遍历

**前序遍历:**若二叉树为空,则空操作返回,否则先访问根结点,然后前序遍历左子树,再前序遍历右子树。
特点:a. 根----->左------->右  b. 根据前序遍历的结果可知第一个访问的必定是root结点。
**中序遍历:**若二叉树为空,则空操作返回,否则从根结点出发 (注意不是先访问根结点),先中序遍历根结点的左子树,再访问根结点,最后再访问根结点的右子树。
**后序遍历:**若而二叉树为空,则空操作返回,否则从根结点出发(注意不是先访问根结点),先后序遍历根结点的左子树,再访问根结点的右子树,最后再访问根结点。

Leetcode 144 : 二叉树的前序遍历
输入: [1,null,2,3]

   1
    \
     2
    /
   3

输出: [1,2,3]

/**
 * Definition for a binary tree node.
 * 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> result; //创建一个容器保存值
        stack<TreeNode* > stack; //创建一个栈
        while(root || !stack.empty()){
            while(root){
                stack.push(root);
                result.push_back(root->val);
                root=root->left;
            }
            if(!stack.empty()){
                root=stack.top()->right; //栈顶元素
                stack.pop(); //出栈
            }
        }
        return result;
    }
};

Leetcode 94: 二叉树的中序遍历
输入: [1,null,2,3]

   1
    \
     2
    /
   3

输出: [1,3,2]


/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val; //结点的值
 *     TreeNode *left; //左子树
 *     TreeNode *right; //右子树
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
using namespace std;
class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
       vector<int> result;
       stack<TreeNode *> stack;
       while(root || !stack.empty()){
           while(root){
               stack.push(root);
               root=root->left;
           }
           if(!stack.empty()){
              result.push_back(stack.top()->val);
              root=stack.top()->right;
              stack.pop();
           }
       }
     return result;
    }
};

Leetcode 145: 二叉树的后序遍历
输入: [1,null,2,3]

   1
    \
     2
    /
   3

输出: [3,2,1]

/**
 * Definition for a binary tree node.
 * 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> result;
        stack<TreeNode *> stack;
        while(root || !stack.empty()){
            while(root){
            stack.push(root);
            result.push_back(root->val);
            root=root->right;
        }
        if(!stack.empty()){
            root=stack.top()->left;
            stack.pop();
        }
     }
     reverse(result.begin(),result.end());
     return result;
    }
};

2.平衡二叉树

一棵高度平衡二叉树定义为:一个二叉树每个节点的左右两个子树的高度差的绝对值不超过1。

Leetcode 110: 平衡二叉树 (simple)
示例1:给定二叉树 [3,9,20,null,null,15,7]

   3
   / \
  9  20
    /  \
   15   7

输出:true
示例2:给定二叉树 [1,2,2,3,3,null,null,4,4]

       1
      / \
     2   2
    / \
   3   3
  / \
 4   4

输出: false

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isBalanced(TreeNode* root) {
        if(!root) return true;
        if(checkDepth(root)==-1) return false;
        else return true;
    }

    int checkDepth(TreeNode* root){ //使用递归即可
        if(!root) return 0;
        int left=checkDepth(root->left);
        if(left==-1) return -1;
        int right=checkDepth(root->right);
        if(right==-1) return -1;
        int diff=abs(left-right);
        if(diff>1) return -1;
        else return 1+max(left,right);
    }
};

3. 二叉树的深度

二叉树的最小深度:最小深度是从根节点到最近叶子结点的最短路径上的节点数量,其中叶子节点指没有子结点的结点。
二叉树的最大深度:最大深度是从根节点到最远叶子结点的最长路径上的节点数量,其中叶子节点指没有子结点的结点。

Leetcode 111: 二叉树的最小深度 (simple)
给定二叉树 [3,9,20,null,null,15,7]

    3
   / \
  9  20
    /  \
   15   7

最小深度为2.

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int minDepth(TreeNode* root) {
        int minDepth=GetDepth(root);
        return minDepth;
    }
    int GetDepth(TreeNode* root) {
        if(!root) return 0;
        int left=GetDepth(root->left);
        int right=GetDepth(root->right);
        if (!left) return 1+right;
        if (!right) return left+1;
        int len_min=min(left+1,right+1);
        return len_min;
    }
};

Leetcode 104: 二叉树的最大深度 (simple)
给定二叉树 [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

返回它的最大深度 3 。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int maxDepth(TreeNode* root) {
        int depth=GetDepth(root);
        return depth;
    }
    int GetDepth(TreeNode* root){
        if(!root) return 0;
        int left=GetDepth(root->left);
        int right=GetDepth(root->right);
        int depth=max(left,right);
        return 1+depth;//必须加1
    }
};
10-05 10:04