leetcode897
这道题用C++来写,在本地执行正常,但是使用OJ判断输出结果是空,暂时不清楚原因。代码如下:
class Solution {
public:
vector<int> V;
//中序遍历
void MidTree(TreeNode node)
{
if (&node != NULL)
{
if (node.left != NULL)
{
MidTree(*node.left);
}
V.push_back(node.val);
if (node.right != NULL)
{
MidTree(*node.right);
}
}
}
TreeNode Join(TreeNode* t, int ct)
{
if (ct == V.size() - )
{
TreeNode tt = TreeNode(V[ct]);
return tt;
}
TreeNode d = TreeNode(V[ct + ]);
TreeNode* dd = &d;
TreeNode n = Join(dd, ct + );
t->right = &n;
return *t;
}
TreeNode* increasingBST(TreeNode* root) {
MidTree(*root);
TreeNode T = TreeNode(V[]);
TreeNode* TT = &T;
TreeNode x = Join(TT, );
return &x;
}
};
保留原有逻辑,修改为C#代码,则通过所有测试,代码如下:
public class Solution
{
public List<int> V = new List<int>();
//中序遍历
public void MidTree(TreeNode node)
{
if (node != null)
{
if (node.left != null)
{
MidTree(node.left);
}
V.Add(node.val);
if (node.right != null)
{
MidTree(node.right);
}
}
} public TreeNode Join(TreeNode t, int ct)
{
if (ct == V.Count() - )
{
TreeNode tt =new TreeNode(V[ct]);
return tt;
}
TreeNode d = new TreeNode(V[ct + ]);
TreeNode n = Join(d, ct + );
t.right = n;
return t;
}
public TreeNode IncreasingBST(TreeNode root)
{
MidTree(root);
TreeNode T =new TreeNode(V[]);
TreeNode x = Join(T, );
return x;
}
}
不知是leetcode的判断机制问题,还是我的C++写法的问题。之后还是尽量使用C#吧。
leetcode897的更多相关文章
- [Swift]LeetCode897. 递增顺序查找树 | Increasing Order Search Tree
Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root o ...
- LeetCode897. 递增顺序查找树
题目 法一.自己 1 class Solution { 2 public: 3 vector<int>res; 4 TreeNode* increasingBST(TreeNode* ro ...
- LeetCode 897. 递增顺序查找树(Increasing Order Search Tree)
897. 递增顺序查找树 897. Increasing Order Search Tree 题目描述 给定一个树,按中序遍历重新排列树,使树中最左边的结点现在是树的根,并且每个结点没有左子结点,只有 ...
随机推荐
- 【spark】示例:求Top值
我们有这样的两个文件 第一个数字为行号,后边为三列数据.我们来求第二列数据的Top(N) (1)我们先读取数据,创建Rdd (2)过滤数据,取第二列数据. 我们用filter()来过滤数据 line. ...
- Ubuntu和Windows文件Samba共享
1.在Ubuntu下配置Samba共享文件夹/work和/work1 1.1.安装samba sudo apt-get install samba
- transition 总结
详情:http://www.css88.com/book/css/properties/transition/transition-property.htm left不能进行transition
- Java 线程的中断机制
今天我们聊聊 Java 线程的中断机制. 线程中断机制提供了一种方法,用于将线程从阻塞等待中唤醒,并作出相应的“受控中断”处理. synchronized (lock) { try { while ( ...
- RIP 知识要点
RIP知识要点: UDP:520 版本:v1(广播包更新) / v2(组播更新 224.0.0.9 ) 度量值:跳数(最多跳15跳,路由为16跳时路由不可达) =================== ...
- linux(redhat) mysql 的安装
教程链接 注意 1.检查版本32/64位的时候 输入 name -a 输出为 Linux localhost.localdomain 2.6.18-53.el5 #1 SMP Wed Oct 10 1 ...
- [QT]QToolBox 抽屉控件初步学习使用
创建一个不带界面的widget工程201701100038. main.c #include "widget.h" #include <QApplication> #i ...
- php str_pad();
<?php $str = "Hello World"; echo str_pad($str,30,"."); ?> 运行实例 定义和用法 str_p ...
- 51nod 1012 最小公倍数LCM
输入2个正整数A,B,求A与B的最小公倍数. 收起 输入 2个数A,B,中间用空格隔开.(1<= A,B <= 10^9) 输出 输出A与B的最小公倍数. 输入样例 30 105 输出 ...
- spring_JavaConfig
从Spring 3起,JavaConfig功能已经包含在Spring核心模块,它允许开发者将bean定义和在Spring配置XML文件到Java类中. interface: package sprin ...