zoukankan      html  css  js  c++  java
  • 783. Minimum Distance Between BST Nodes

    https://leetcode.com/problems/minimum-distance-between-bst-nodes/description/

    /**
     * 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 res = INT_MAX;
        int minDiffInBST(TreeNode* root) {
            dfs(root);
            return res;
        }
        TreeNode* dfs(TreeNode* root, TreeNode* pre = NULL) {
            if (root == NULL)
                return NULL;
            
            if (root->left) {
                TreeNode* leftLast = dfs(root->left, pre);
                res = min(res, root->val - leftLast->val);
            }
            else if (pre != NULL) {
                res = min(res, root->val - pre->val);
            }
            
            if (root->right) {
                return dfs(root->right, root);
            }
            return root;
        }
    };
  • 相关阅读:
    mfc启动画面
    个人冲刺第十天
    个人冲刺第九天
    个人冲刺第八天
    个人冲刺第七天
    个人冲刺第六天
    新一周冲刺计划2
    新一周冲刺
    创意1
    团队绩效与目标
  • 原文地址:https://www.cnblogs.com/JTechRoad/p/8994845.html
Copyright © 2011-2022 走看看