题目大意:给你一个数N,输出N的二进制形式上为1的数位(从右至左)
思路:每次(N&1)判断末尾是否为1,为1则存入数组ans[]中,不为1则
不存,之后数位自增,N向右移一位,继续判断末尾……...
分类:
其他好文 时间:
2014-11-27 00:22:28
阅读次数:
288
题目:二叉树中有两个节点对换了值,恢复他们。思路:因为中序遍历是有序的,如果中序遍历后的数组出现乱序,说明就是交换的。从前往后,第一次乱序的第一个,后最后一次乱序的后一个,然后把这两个值对换就好了。想了个非常挫的办法。先中序遍历Binary Tree Inorder Traversal,然后在数组中...
分类:
其他好文 时间:
2014-11-27 00:08:40
阅读次数:
240
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100".
我的C++实现代码如下:
string addBinary(string a, string b) {
string sum;
int c...
分类:
其他好文 时间:
2014-11-26 22:43:30
阅读次数:
196
问题描述:
Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3},
1
2
/
3
return [1,3,2].
Note: Recursive solution...
分类:
其他好文 时间:
2014-11-26 22:39:25
阅读次数:
227
1 class Solution { 2 public: 3 string addBinary(string a, string b) { 4 if(a==""&&b=="") return ""; 5 if(a=="") return b; 6...
分类:
其他好文 时间:
2014-11-26 22:22:01
阅读次数:
198
Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never diffe...
分类:
其他好文 时间:
2014-11-26 19:04:36
阅读次数:
148
这些主要是从 "小骆驼" 书上粘贴或者摘抄出来的, 个人认为需要记的语法知识"在某些情况下, 你可能需要在一台机器上写程序, 再传送到另一台机器上运行.这时候, 请使用"文本模式(text mode)" 或者"ASCII模式(ASCIImode)". 记住, 千万不能是"二进制模式(binary m...
分类:
其他好文 时间:
2014-11-26 18:32:09
阅读次数:
166
给出一个已排序的数组,将其转化为二叉查找树(BST)。思路:取数组中间元素为根结点的value,则数组左侧、右侧分别为BST的左子树、右子树。递归可求解。代码如下: 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * ...
分类:
其他好文 时间:
2014-11-26 18:13:30
阅读次数:
206
Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Note:A solution using O(n) space is ...
分类:
其他好文 时间:
2014-11-26 13:58:49
阅读次数:
255
TreeYou are to determine the value of the leaf node in a given binary tree that is the terminal node of a path of least value from the root of the bin...
分类:
其他好文 时间:
2014-11-26 13:50:09
阅读次数:
231