zoukankan      html  css  js  c++  java
  • LeetCode-C#实现-二叉树/二叉搜索树(#98/104/111/230)

    98. Validate Binary Search Tree

    验证二叉搜索树

    解题思路

    ①整个递归过程类似于后序遍历,先遍历到了左子树的叶节点,开始比较,然后在返回时递归右节点,右节点符合后返回,开始验证父节点,以此类推。

    //整体思路类似于后序遍历二叉树
    public class Solution {
        //设一个最小值
        double min=double.MinValue;
        public bool IsValidBST(TreeNode root) {
            //节点为空则返回
            if(root==null)return true;
            //遍历节点的左节点一直到叶节点
            if(IsValidBST(root.left)){
                //节点值必须大于最小值,然后更新最小值为节点的值
                if(min<root.val){
                    min=root.val;
                    //遍历右节点,右节点若也为true,则该节点的左右子节点符合BST回到上一级
                    return IsValidBST(root.right);
                }
            }
            return false;
        }
    }

    104. Maximum Depth of Binary Tree

    二叉树的最大深度

    解题思路

    深度优先搜索,将每一层的深度传给下一层,直到传到叶节点,将深度存入集合。最后取出集合中最大的数即为最大深度。

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     public int val;
     *     public TreeNode left;
     *     public TreeNode right;
     *     public TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public int MaxDepth(TreeNode root) {
            //若无根节点返回深度为0
            if(root==null) return 0;
            //声明集合保存各个叶节点的深度
            List<int> depthList = new List<int>();
            //搜索树的深度,传入根节点、集合、根节点深度
            Search(root,depthList,1);
            //获取集合中最大深度
            int maxDepth = int.MinValue;
            foreach(int depth in depthList)
                if (depth > maxDepth) maxDepth = depth;
            return maxDepth; 
        }
        public void Search(TreeNode root, List<int> depthList, int rootDepth){
            //如果节点左右子节点都不存在则为叶节点,将其深度存入集合
            if (root.left == null&& root.right == null)
                depthList.Add(rootDepth);
            //否则,继续递归遍历节点的左右节点,将节点的深度传给其左右子节点
            if (root.left != null)
                Search(root.left, depthList, rootDepth + 1);
            if (root.right != null)
                Search(root.right, depthList, rootDepth + 1);
        }
    }

     

    111. Minimum Depth of Binary Tree

    二叉树的最小深度

    解题思路

    与最大深度类似,取出集合中最小的值即可

    222. Count Complete Tree Nodes

    完全二叉树的节点个数

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     public int val;
     *     public TreeNode left;
     *     public TreeNode right;
     *     public TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public int CountNodes(TreeNode root) {
            if(root==null)return 0;//判空返回
            //获取左右子树高度
            int leftHeight=GetHeight(root.left);
            int rigthHeight=GetHeight(root.right);
            //相等说明左子树一定是满的,返回左子树节点数和右子树递归,否则先计算左子树递归
            if(leftHeight==rigthHeight)return (1<<leftHeight)+CountNodes(root.right);
            else return (1<<rigthHeight)+CountNodes(root.left);
        }
        public int GetHeight(TreeNode node){
            //左右子树获取高度都是取其左子树遍历
            //左右子树若相等,最后的叶节点一定在右子树,若不相等则右子树高度小于左子树
            int height = 0;
            while(node != null) {
                height++;
                node = node.left;
            }
            return height;
        }
    }

    230. Kth Smallest Element in a BST

    二叉搜索树中第K小的元素

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     public int val;
     *     public TreeNode left;
     *     public TreeNode right;
     *     public TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public int KthSmallest(TreeNode root, int k) {
            List<int> list=new List<int>();//存储节点值的集合
            InOrder(list,root);//中序遍历二叉搜索树,集合中元素一定是升序的
            return list[k-1];//返回dik个元素
        }
        public void InOrder(List<int> list,TreeNode node){
            if(node==null)return;
            PreOrder(list,node.left);
            list.Add(node.val);
            PreOrder(list,node.right);
        }
    }
  • 相关阅读:
    洛谷-P5709 【深基2.习6】Apples Prologue
    右键添加功能
    配置Java环境变量遇到的坑
    mysql安装
    页面置换算法
    程序员画图软件
    Java中传值和传引用
    计算机网络之链路层
    Python开发【第十五篇】模块的导入
    Python开发【第十四篇】装饰器
  • 原文地址:https://www.cnblogs.com/errornull/p/10001003.html
Copyright © 2011-2022 走看看