leetcode题解日练--2016.09.02

###不给自己任何借口

今日题目:

1、判断是否BST

2、二叉树的后序遍历

今日摘录:

人们宁愿去关心一个蹩脚电影演员的吃喝拉撒和鸡毛蒜皮,而不愿了解一个普通人波涛汹涌的内心世界……

——路遥《平凡的世界》

98. Validate Binary Search Tree | Difficulty: Medium

Given a binary tree, determine if it is a valid binary search tree (BST).

Assume a BST is defined as follows:

The left subtree of a node contains only nodes with keys less than the node’s key.
The right subtree of a node contains only nodes with keys greater than the node’s key.
Both the left and right subtrees must also be binary search trees.
Example 1:
2
/ \
1 3
Binary tree [2,1,3], return true.
Example 2:
1
/ \
2 3
Binary tree [1,2,3], return false.

tag:树|DFS
题意:判断一棵二叉树是否合法

思路:
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:
    bool isValidBST(TreeNode* root) {
        if(!root)   return true;
        vector<int>res;
        stack<TreeNode*>nodes;
        TreeNode*cur = root;
        while(cur || !nodes.empty())
        {
            while(cur)
            {
                nodes.push(cur);
                cur = cur->left;
            }
            cur = nodes.top();
            res.push_back(cur->val);
            nodes.pop();
            cur = cur->right;
        }
        for(int i=0;i<res.size()-1;i++)
        {
            if(res[i]>=res[i+1])   return false;
        }
        return true;
    }
};

结果:16ms

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:
    bool isValid(TreeNode*root,long min,long max)
    {
        if(!root)   return true;
        if(root->val <= min || root->val >= max) return false;
        return isValid(root->left,min,root->val) && isValid(root->right,root->val,max);
    }
    bool isValidBST(TreeNode* root) {
        if(!root)   return true;
        return isValid(root,LONG_MIN,LONG_MAX);
    }
};

结果:12ms

145. Binary Tree Postorder Traversal | Difficulty: Hard

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].

tag:树|栈

题意:后序遍历二叉树
思路:
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>res;
        if(!root)   return res;
        postorder(root,res);
        return res;
    }
    void postorder(TreeNode* root,vector<int>&res)
    {
        if(!root)   return;
        postorder(root->left,res);
        postorder(root->right,res);
        res.push_back(root->val);
    }
};

结果:0ms

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:
    vector<int> postorderTraversal(TreeNode* root) {
        vector<int>res;
        if(!root)   return res;
        stack<TreeNode*> nodes;
        nodes.push(root);
        while(!nodes.empty())
        {
            TreeNode* cur = nodes.top();
            nodes.pop();
            res.push_back(cur->val);
            if(cur->left) nodes.push(cur->left);
            if(cur->right) nodes.push(cur->right);
        }
        reverse(res.begin(),res.end());
        return res;
    }

};

结果:4ms

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值