Task23——不同的二叉搜索树 II(带更新)

news/2024/7/8 4:42:48

题目:

给定一个整数 n,生成所有由 1 ... n 为节点所组成的二叉搜索树。

示例:

输入: 3
输出:
[
  [1,null,3,2],
  [3,2,null,1],
  [3,1,null,null,2],
  [2,1,3],
  [1,null,2,null,3]
]
解释:
以上的输出对应以下 5 种不同结构的二叉搜索树:

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

解题:

/**
 * 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 IList<TreeNode> GenerateTrees(int n)
        {
            var preResult = new List<TreeNode>();
            if (n == 0)
            {
                return preResult;
            }

            preResult.Add(null);

            for (int i = 1; i <= n; i++)
            {
                var currentResult = new List<TreeNode>();

                foreach (var node in preResult)
                {
                    //放在顶点
                    var newHeadNode = new TreeNode(i)
                    {
                        left = TreeCopy(node)
                    };
                    currentResult.Add(newHeadNode);

                    //放在上一次结果的右顶点
                    //count用来记录下一次遍历到哪个顶点
                    int count = 1;
                    while (count > 0)
                    {
                        var newTreeNode = TreeCopy(node);
                        var targetNode = newTreeNode;
                        var newRigthNode = new TreeNode(i);

                        for (int j = 1; j < count; j++)
                        {
                            targetNode = targetNode.right;
                        }
                        if (targetNode != null)
                        {
                            var tempNode = targetNode.right;
                            targetNode.right = newRigthNode;
                            newRigthNode.left = tempNode;
                            currentResult.Add(newTreeNode);
                            count++;
                        }
                        else
                        {
                            count = -1;
                        }

                    }

                    preResult = currentResult;
                }
            }

            return preResult;
        }

        private TreeNode TreeCopy(TreeNode treeNode)
        {
            if (treeNode == null)
            {
                return null;
            }
            var result = new TreeNode(treeNode.val)
            {
                left = TreeCopy(treeNode.left),
                right = TreeCopy(treeNode.right)
            };
            return result;
        }
}

来源:

力扣(LeetCode)


http://www.niftyadmin.cn/n/1990971.html

相关文章

Task24——恢复二叉树(待更新)

题目&#xff1a; 二叉搜索树中的两个节点被错误地交换。 请在不改变其结构的情况下&#xff0c;恢复这棵树。 示例 1: 输入: [1,3,null,null,2] 1 / 3 \ 2 输出: [3,1,null,null,2] 3 / 1 \ 2 示例 2: 输入: [3,1,4,null,null,2] 3 / \ 1 4 / …

C++编程杂谈之二:面向对象

C编程杂谈之二&#xff1a;面向对象作者/xulion 软件开发是一个极其复杂的过程&#xff0c;一段小的代码我们可以快速、准确的完成&#xff0c;但是当你面对的是一个庞大的软件系统的时候&#xff0c;你是否有不知所措的感觉呢&#xff1f;在我们使用C的年代里面&#xff0c;编…

使用 Animation dll 捕获按键

Capturing key events using Animation dll使用 Animation dll 捕获按键Platform -----------------------------------S60 3rd Edition, FP1S60 3rd Edition, FP2S60 5th Edition Tested on devices-----------------------------------Tested on Nokia N95,Navi 6210,Nok…

C++编程杂谈之一:编译器

C编程杂谈之一&#xff1a;编译器作者/xulion 网上有很多各种编译器的优劣比较的东西&#xff0c;我写这些东西并不是想支持或否定某些东西&#xff0c;因为我始终认为在编程的领域中&#xff0c;我只是一个初学者&#xff0c;并没有资格来评判什么&#xff08;况且我也不…

Task25——买卖股票的最佳时机

题目&#xff1a; 给定一个数组&#xff0c;它的第 i 个元素是一支给定股票第 i 天的价格。 设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易&#xff08;多次买卖一支股票&#xff09;。 注意&#xff1a;你不能同时参与多笔交易&#xff08;你必须…

Nokia论坛技术资料汇

http://discussion.forum.nokia.com/forum/showthread.php?t60202&page40

Task26——判断子序列

题目&#xff1a; 给定字符串 s 和 t &#xff0c;判断 s 是否为 t 的子序列。 你可以认为 s 和 t 中仅包含英文小写字母。字符串 t 可能会很长&#xff08;长度 ~ 500,000&#xff09;&#xff0c;而 s 是个短字符串&#xff08;长度 <100&#xff09;。 字符串的一个子…

PS 键 电话状态及指示器 API

PS Keys for Call Status & Indicators APIPS 键 电话状态及指示器 APINote! This API is not part of the public SDK. It can be found in the SDK API Plug-in. 这个API不是SDK的一部分&#xff0c;可以在SDK插件包中找到。 PS Keys for Call Status & Indicators …