c*****n 发帖数: 96 | 1 For the first question:
// Assumption: all node's sibling property is null before calling
// the function.
void linkSibling(Node root){
if (root == null) return;
if (root.left != null){
root.left.sibling = root.right;
}
if (root.right != null ){
root.right.sibling = root.sibling == null ? null : root.sibling.left;
}
linkSibling(root.left);
linkSibling(root.right);
} |
|
c*****n 发帖数: 96 | 2 For the first question:
// Assumption: all node's sibling property is null before calling
// the function.
void linkSibling(Node root){
if (root == null) return;
if (root.left != null){
root.left.sibling = root.right;
}
if (root.right != null ){
root.right.sibling = root.sibling == null ? null : root.sibling.left;
}
linkSibling(root.left);
linkSibling(root.right);
} |
|
p*****2 发帖数: 21240 | 3
我写了一个。
Iterator it=null;
Predicate pr=null;
T next=null;
Iterator conditionIterator(Iterator input, Predicate pred)
{
it=input;
pr=pred;
hasNext();
}
T next()
{
T ret=next;
hasNext();
return ret;
}
T hasNext()
{
if(next==null)
{
while(it.hasNext())
{
next=it.hasNext();
if(pr.accept(next))
break;
else
next=null;
}
}
return next!=null;
} |
|
b******u 发帖数: 81 | 4 public static Node RemoveDuplicates(Node node)
{
Node result = null, resultCurrent = null, current = node;
bool skipCurrent = false;
while (current != null )
{
if (current.Next != null && current.Num == current.Next.Num)
{
skipCurrent = true;
}
else
{
if (!skipCurrent)
{
if (result == null)
{
result = current;
resultCurren... 阅读全帖 |
|
I****h 发帖数: 33 | 5 PNode trim_list(PNode begin)
{
Node head = {0};
PNode anchor = NULL;
PNode prev = NULL;
PNode curr = NULL;
int trim = 0;
if ((begin == NULL) || (begin->key != 0))
{
head.key = 0;
}
else
{
head.key = 1;
}
head.next = begin;
anchor = &head;
prev = &head;
curr = begin;
while (curr != NULL)
{
if (prev->key != curr->key)
{
if (trim == 0)
{
anchor = prev;
... 阅读全帖 |
|
w****x 发帖数: 2483 | 6 /*
Serialize/DeSerialize a tree
*/
struct NODE
{
int nVal;
vector vec;
NODE(int n) : nVal(n) {}
};
void _inner_serial(NODE* pNode, char*& p)
{
if (NULL == pNode)
return;
*p++ = pNode->vec.size();
*p++ = pNode->nVal;
for (vector::iterator it = pNode->vec.begin();
it != pNode->vec.end(); it++)
_inner_serial(*it, p);
}
const char* Serialize(NODE* pRoot, char mem[])
{
if (NULL == mem || NULL == pRoot)
return NULL;
char... 阅读全帖 |
|
s******o 发帖数: 328 | 7 inline void connectOne( Node *n, Node *&nHead, Node *&nTmp ) {
if ( n == NULL ) return;
if ( nHead == NULL ) { nHead = nTmp = n; }
else { nTmp->next = n; nTmp = nTmp->next; }
}
void connect(Node *root) {
Node * cHead = root;
while ( cHead != NULL ) {
Node * nHead = NULL, * nTmp = NULL, *cTmp = cHead;
while ( cTmp != NULL ) {
connectOne( cTmp->left, nHead, nTmp );
connectOne( cTmp->right, ... 阅读全帖 |
|
h*****f 发帖数: 248 | 8 #include
struct Node {
int value;
Node* pLeft;
Node* pRight;
};
int findMaxSubTree(Node* pNode, int& maxSoFar) {
if (!pNode) return 0;
int leftSum = findMaxSubTree(pNode->pLeft, maxSoFar);
int rightSum = findMaxSubTree(pNode->pRight, maxSoFar);
int currentSum = pNode->value + std::max(0, leftSum) + std::max(0,
rightSum);
maxSoFar = std::max(currentSum, maxSoFar);
return currentSum;
}
int main() {
Node node4 = {4, NULL, NULL};
Node node3 = {3, NULL, NULL... 阅读全帖 |
|
c****7 发帖数: 13 | 9 写了个第一题的DFS的非递归解法, 请高手多指教
public static TreeNode findDeepestNode(TreeNode root){
if(root ==null) return root;
int maxDepth = 1;
TreeNode deepestNode = root;
// The previous node we just traversed
TreeNode preNode=null;
// The top item in the stack
TreeNode curNode;
Stack nodeStack = new Stack();
nodeStack.push(root);
// exit until the stack is empty
while(!nodeStack.isEmpty())... 阅读全帖 |
|
P*******U 发帖数: 203 | 10 不用转的,可以直接搞
public class Solution {
public TreeNode sortedListToBST(ListNode head) {
// Start typing your Java solution below
// DO NOT write main() function
if(head == null)
return null;
if(head.next == null)
return new TreeNode(head.val);
ListNode head1, head2;
head1 = head;
head2 = head;
while(head2!=null && head2.next!=null){
head1 = head1.next;
head2... 阅读全帖 |
|
l*******b 发帖数: 2586 | 11 #include
using namespace std;
struct IntNode {
int a;
int b;
IntNode *next;
IntNode(int low, int high) : a(low), b(high), next(NULL) {}
};
struct HeadsNode {
IntNode *list;
HeadsNode *next;
HeadsNode() : next(NULL) {}
};
class MergeInt {
public:
void merger(HeadsNode *h, HeadsNode *t) {
if(h == t) return;
HeadsNode *mid = middle(h,t);
merger(h, mid);
merger(mid->next, t);
h->list = mergeTwoList(h->list, (mid->next... 阅读全帖 |
|
e******i 发帖数: 106 | 12 Hi leetcode上我有道题看不明白,求教各位大神。
是关于flatten binary tree to linkedlist那道。
我看到一个答案。
public class Solution {
03 public void flatten(TreeNode root) {
04 if(root==null)
05 return;
06
07 TreeNode curr=null;
08 Stack trees=new Stack();
09 trees.add(root);
10 while(!trees.empty())
11 {
12 TreeNode parent=trees.pop();
13
14 if(parent.right!=null)
15 {
16 ... 阅读全帖 |
|
b*******3 发帖数: 145 | 13 我用了两个que和1个stack,就没有多少null check,应该还可以优化。代码如下:
public boolean isSymmetric1(TreeNode root) {
// Start typing your Java solution below
// DO NOT write main() function
if(root == null){
return true;
}else{
Queue que = new LinkedList();
Queue que1 = new LinkedList();
Stack st = new Stack();
que.add(root);
st.push(root);
whi... 阅读全帖 |
|
c********t 发帖数: 5706 | 14 土了,发现原来java stack和queue都允许null,只是我自己代码开始逻辑有问题。修改
后的DFS如下,过了。
public boolean isSymmetric2(TreeNode root) {
// Start typing your Java solution below
// DO NOT write main() function
if (root == null)
return true;
Stack s1 = new Stack();
Stack s2 = new Stack();
s1.push(root.left);
s2.push(root.right);
while (!s1.isEmpty() && !s2.isEmpty()) {
TreeNode n1 = s1.pop();
TreeNode n2 = s2.pop();
if (n1 =... 阅读全帖 |
|
b*******3 发帖数: 145 | 15 我用了两个que和1个stack,就没有多少null check,应该还可以优化。代码如下:
public boolean isSymmetric1(TreeNode root) {
// Start typing your Java solution below
// DO NOT write main() function
if(root == null){
return true;
}else{
Queue que = new LinkedList();
Queue que1 = new LinkedList();
Stack st = new Stack();
que.add(root);
st.push(root);
whi... 阅读全帖 |
|
c********t 发帖数: 5706 | 16 土了,发现原来java stack和queue都允许null,只是我自己代码开始逻辑有问题。修改
后的DFS如下,过了。
public boolean isSymmetric2(TreeNode root) {
// Start typing your Java solution below
// DO NOT write main() function
if (root == null)
return true;
Stack s1 = new Stack();
Stack s2 = new Stack();
s1.push(root.left);
s2.push(root.right);
while (!s1.isEmpty() && !s2.isEmpty()) {
TreeNode n1 = s1.pop();
TreeNode n2 = s2.pop();
if (n1 =... 阅读全帖 |
|
c*****2 发帖数: 34 | 17 第一题这样做可以吗?
做pre-order traversal,如果两个node相同比较parent list是否相同。
然后递归看左右子树是否相同。
public class TreeNode {
TreeNode left;
TreeNode right;
int val;
ArrayList parents = new ArrayList();
public TreeNode(int val) {this.val = val;}
}
public boolean isIdentical(TreeNode t1, TreeNode t2) {
if (t1 == null && t2 == null) return true;
if (t1 == null && t2 != null) return false;
if (t1 != null && t2 == null) return false;
if (t1.val != t2.val) return false;
... 阅读全帖 |
|
w****x 发帖数: 2483 | 18 /*
Serialize/DeSerialize a tree
*/
struct NODE
{
int nVal;
vector vec;
NODE(int n) : nVal(n) {}
};
void _inner_serial(NODE* pNode, char*& p)
{
if (NULL == pNode)
return;
*p++ = pNode->vec.size();
*p++ = pNode->nVal;
for (vector::iterator it = pNode->vec.begin();
it != pNode->vec.end(); it++)
_inner_serial(*it, p);
}
const char* Serialize(NODE* pRoot, char mem[])
{
if (NULL == mem || NULL == pRoot)
return NULL;
char... 阅读全帖 |
|
w****x 发帖数: 2483 | 19 /*
Serialize/DeSerialize a tree
*/
struct NODE
{
int nVal;
vector vec;
NODE(int n) : nVal(n) {}
};
void _inner_serial(NODE* pNode, char*& p)
{
if (NULL == pNode)
return;
*p++ = pNode->vec.size();
*p++ = pNode->nVal;
for (vector::iterator it = pNode->vec.begin();
it != pNode->vec.end(); it++)
_inner_serial(*it, p);
}
const char* Serialize(NODE* pRoot, char mem[])
{
if (NULL == mem || NULL == pRoot)
return NULL;
char... 阅读全帖 |
|
p*****2 发帖数: 21240 | 20 class MyIterator(v:Vector[Vector[Int]]){
private[this] val vector1=v.iterator
private[this] var vector2:Iterator[Int]=null
def hasNext():Boolean={
if(vector2!=null && vector2.hasNext) return true
vector2=null
while(vector1.hasNext && (vector2==null || !vector2.hasNext))
{
val next=vector1.next
if(next!=null) vector2=next.iterator
}
vector2!=null && vector2.hasNext
}
def next():Int={
... 阅读全帖 |
|
w****x 发帖数: 2483 | 21
没意思
单链表的quick sort就可以了
struct NODE
{
int nVal;
NODE* pNext;
NODE(int n) : nVal(n), pNext(NULL) {}
};
void sort(NODE* pNode, int nLen)
{
if (NULL == pNode || nLen <= 0)
return;
int nCount = 0;
int nPivot = pNode->nVal;
NODE* pPrev = pNode;
NODE* pIterBeg = pPrev->pNext;
NODE* pIterEnd = pIterBeg;
while (NULL != pIterEnd)
{
if (pIterEnd->nVal < nPivot)
{
swap(pIterBeg->nVal, pIterEnd->nVal);
pPrev = pPrev-... 阅读全帖 |
|
x*********w 发帖数: 533 | 22 quick sort版本
struct NODE
{
int nVal;
NODE* pNext;
NODE(int n) : nVal(n), pNext(NULL) {}
};
void sort(NODE* pNode, int nLen)
{
if (NULL == pNode || nLen <= 0)
return;
int nCount = 0;
int nPivot = pNode->nVal;
NODE* pPrev = pNode;
NODE* pIterBeg = pPrev->pNext;
NODE* pIterEnd = pIterBeg;
while (NULL != pIterEnd)
{
if (pIterEnd->nVal < nPivot)
{
swap(pIterBeg->nVal, pIterEnd->nVal);
pPrev = pPrev->pNext;
... 阅读全帖 |
|
p*****p 发帖数: 379 | 23 heap直接放node就行了啊,贴个java的,c++也一样的
public class Solution {
public ListNode mergeKLists(ArrayList lists) {
// Start typing your Java solution below
// DO NOT write main() function
ListNode root = null;
ListNode current = null;
if (lists.isEmpty()) return null;
Queue queue = new PriorityQueue(lists.size(), new
Comparator() {
public int compare(ListNode n1, ListNode n2) {
return n1.val - n2.val;... 阅读全帖 |
|
s******n 发帖数: 226 | 24 Insert into BST and get score.
This is runnable code.
class node{
int val;
int below;
node left;
node right;
node(int val){this.val = val; left = right = null; below = 0;}
}
class Tree{
node root;
Tree(){root = null;}
int insert(int val){
node N = root;
if(N == null){
root = new node(val);
return 0;
}
int score = 0;
node par = null;
while(N!=null){
if(val <= N.val){
... 阅读全帖 |
|
d******l 发帖数: 98 | 25 第一题,我用java作: 只需要遍历一次就够了 O(n)time
首先,定义一个Node head=null, runner=null; int value=carry (进位1 or 0)
接下来用while循环遍历
while(N1!=null && N2!=null){
定义一个新的临时 Node. Node temp=new Node(); //先做个位数节点,第二次遍历
是 就是 十位数节点...
再为遍历作下工作:Node next1=N1.next; Node next2=N2.next;
如果N1 N2 不为null, 把value+=N1.data; value+=N2.data;
if(value>=10) carry=1; else carry=0;
temp.data=value%10; //这是取个位数
接下来按数序插入高位的Node (十 百 千 万位 递增顺序)
if(head==null){ head=temp; runner=head; }
else{ runner.next=temp; runner... 阅读全帖 |
|
r**h 发帖数: 1288 | 26 class Solution {
public:
int maxDepth(TreeNode *root) {
if(root == NULL) return 0;
stack st;
st.push(root);
TreeNode *prev=NULL, *cur;
int maxHeight = 0;
while(st.size()>0){
cur = st.top();
if(prev==NULL || prev->left==cur || prev->right==cur){
if(cur->left != NULL){
st.push(cur->left);
}
else if(cur->right != NULL){
... 阅读全帖 |
|
s********r 发帖数: 403 | 27 大牛不敢。
我只是写过一些简单的 non-blocking algorithm。
这些 non-blocking 算法是 Multiple-Thread Multiple Data 的编程模式,用于多核
系统,但其执行效率与体系结构,尤其是 cache 结构相关。还牵涉到 memory
consistent model。在c++11 中,很多 atomic operation 是为这个准备的。
下面的 code 仅仅代表一个思路上的描述:
void enq(Node *queue, T val)
{
Node * node = new node(val);
node->next = NULL;
Node * tail = NULL, *last = NULL;
for (;;)
{
tail = queue->gettail(); //atomic get
last = tail->next;
if (tail == queue->gettail()) //Are we still there?
{... 阅读全帖 |
|
t*****9 发帖数: 569 | 28 这段程序被鄙视了,我也觉得太繁琐,写得不够简练。
麻烦各位review一下,提点儿改进意见,我也学习一下。
public LinkNode AddLinkedList(LinkNode ln1, LinkNode
ln2, int carry)
{
LinkNode result = new LinkNode();
LinkNode idx1 = ln1;
LinkNode idx2 = ln2;
if ((idx1 == null) && (idx2 == null))
{
if (carry == 0) return null;
else
{
LinkNode node = new LinkNode... 阅读全帖 |
|
S*******C 发帖数: 822 | 29 package com.cdd.jdbc;
import java.sql.*;
public class CreateConnection {
public Connection conn=null;
public ResultSet rs=null;
public Statement stmt=null;
public CreateConnection() { //构造方法
}
//获取数据库连接方法
public Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://mydbinstance.XXXXXXX.
us-east-1.rds.amazonaws.com:3306/db_database05","root","11111");
//不管用上... 阅读全帖 |
|
s*******n 发帖数: 305 | 30 public int heightIterativeDFS() {
if (root==null) return 0;
Stack stack = new Stack();
stack.push(root);
int height=0;
TreeNode prev = null;
while(!stack.isEmpty()) {
TreeNode current=stack.peek();
if (prev==null || prev.left==current || prev.right==current) {
if (current.left!=null) stack.push(current.left);
else if (current.right!=null) stack.p... 阅读全帖 |
|
k*******2 发帖数: 84 | 31 以前一直用C++刷题,imagong只能用Java, 我写了一个二叉树的最低公共节点:
下面的程序不对,但是我把第一行改成root.val == p.val || root.val == q.val之后
可以了
难道 "=="在Java里面不是看reference是否相等吗?
请各位不吝赐教,小妹不胜感激 :)
public class Solution{
TreeNode LCA(TreeNode root, TreeNode p, TreeNode q){
if(root == null || root == p || root == q)
return root;
TreeNode left = null;
TreeNode right = null;
left = LCA(root.left, p, q);
right = LCA(root.right, p, q);
if(left != null && right != nu... 阅读全帖 |
|
g***j 发帖数: 1275 | 32 经典题目,就是一个linked list,每个节点有一个指针指向random的节点,然后呢,
copy这个list,
基本思路就是在每个节点后面插入一个同样的节点,然后设置好random的指针,最后把
这个list exact出来
举例: 1->2->3->4 其中 1->3 2->4
变成 1->1'->2->2'->3->3'->4->4' 其中 1'->3' 2'->4'
始终有runtime error, 被搞死了,谢了!
class Solution {
public:
RandomListNode *copyRandomList(RandomListNode *head) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
RandomListNode * p = head;
if(p == NULL) return NULL;
... 阅读全帖 |
|
j*******t 发帖数: 223 | 33 sedgewick的书里有讲过很简单的trie。
public class TrieST {
private static final int R = 256; // extended ASCII
private Node root = new Node();
private static class Node {
private Object val;
private Node[] next = new Node[R];
}
public boolean contains(String key) {
return get(key) != null;
}
public Value get(String key) {
Node x = get(root, key, 0);
if (x == null) {
return null;
}
return (Value) x.val;
}
... 阅读全帖 |
|
j*******t 发帖数: 223 | 34 sedgewick的书里有讲过很简单的trie。
public class TrieST {
private static final int R = 256; // extended ASCII
private Node root = new Node();
private static class Node {
private Object val;
private Node[] next = new Node[R];
}
public boolean contains(String key) {
return get(key) != null;
}
public Value get(String key) {
Node x = get(root, key, 0);
if (x == null) {
return null;
}
return (Value) x.val;
}
... 阅读全帖 |
|
p*****3 发帖数: 488 | 35 给个我以前写的吧,快速排序版本的
struct NODE
{
int nVal;
NODE* pNext;
NODE(int n) : nVal(n), pNext(NULL) {}
};
void sort(NODE* pNode, int nLen)
{
if (NULL == pNode || nLen <= 0)
return;
int nCount = 0;
int nPivot = pNode->nVal;
NODE* pPrev = pNode;
NODE* ... 阅读全帖 |
|
m********c 发帖数: 105 | 36 写了一个iterative method,参考了这个 http://www.chiark.greenend.org.uk/~sgtatham/algorithms/listsort.html
ListNode *sortList(ListNode *head) {
if (head == NULL)
return head;
int width = 1, firstSize, secondSize;
ListNode *tail;
while (true) {
ListNode *first = head;
ListNode *curr;
int nmerge = 0;
tail = NULL;
while (first) {
ListNode *second = first;
firstSize = 0;
nmerge++;
for (int i=0; i阅读全帖 |
|
m**********4 发帖数: 774 | 37 多谢LZ分享。试着写了一会儿,花了挺长时间才想清楚。估计要是被面肯定挂了,时间
太长要是三姐再干扰干扰根本没可能想清楚。
JAVA:
Iterative version, 应该和reverse linkedlist差不多,但要多记录几个NODE的值
public TreeNode convert(TreeNode tn){
if (tn == null)
return tn;
TreeNode curr = tn;
TreeNode prevLeft = null;
TreeNode prevRight = null;
while (curr != null){
TreeNode currLeft = curr.left;
TreeNode currRight = curr.right;
curr.left = prevRight;
... 阅读全帖 |
|
w****3 发帖数: 110 | 38 我贴一个,另外可以直接用linkedhashmap做,设为accessOnly直接用
removeeldestentry,只要几行就能写完。下面的是自己用doubleLinkedList做的。
public class LRUCache {
HashMap data = new HashMap
DoubleLinkedListNode>();
int cap;
int count = 0;
DoubleLinkedListNode head = null;
DoubleLinkedListNode helper;
public LRUCache(int capacity) {
cap = capacity;
helper = new DoubleLinkedListNode(-1,-1);
head = helper;
}
public int get(int key) {
... 阅读全帖 |
|
f******h 发帖数: 45 | 39 也找工作了一段时间了,从版上学了很多,上周G家面完了,求个bless。
之前的一些都挂了,还在继续找其他的。等定下来之后一定发面经回报本版。
谢谢大家啦!!
1. http://www.mitbbs.com/article_t/JobHunting/32005597.html
1) Implement a simple calculator (+,-,*,/);
2) Implement "+1" for a large integer;
3) How to match Ads to users;
4) How to extract useful information from a forum webpage (list all
kinds of useful signal you can think of)
5) How to detect the duplicate HTML pages (large scale);
6) Find all the paths between two places on Google map;
7)... 阅读全帖 |
|
l*****a 发帖数: 14598 | 40 两年没面试了,想出来找找面试的感觉然后冲击一下版上公认的那些dream company,没
想到一出来就遭受当头一棒。
一月初的时候在linkedin上收到V公司recruiter的来信,说是在多伦多搞一个event,问
我有没有兴趣。
于是先做了一个online test,本来说还要有一个phone screen的,正安排的过程中一开
始联系我的猎头说直接来吧
这是2月中旬了,接下来的5-6周忙着手头的一个小project,业余时间大多给公司+网络了
,没什么心思准备。
3月21日开始,项目没什么问题了,开始刷了两周的leetcode,别的几乎什么也没看就匆
忙上阵了,本来只希望
积累点interview经验,为其他公司面试做准备。。。谁成想,两轮就被踢出来了。。。
旅程就不顺利,12点从家出来最后11点才进旅馆,第二天7:30就开场。
先是原定直飞的flight被cancel,然后弄了一个1 stop的,前后两段都分别延误了不少
时间
时间不定结果都没来得及吃晚饭。9点到多伦多,租车花了半天,冒着大雨开了几十公
里,
11点进旅馆屋里连水都没有,又饿又累就睡了
online tes... 阅读全帖 |
|
s**9 发帖数: 207 | 41 有点错,是 父结点的第一个有子结点的sibling的子结点
//for the case where Node current is its parent's right child, or it's the
left child and its parent has no right child:
Node p=parent;
while(p.left==null && p.right==null && p.sibling!=null){
p=p.sibling
}
if(p.left !=null){
current.sibling=p.left;
}else if(p.right !=null){
current.sibling=p.right;
}else{
current.sibling=null;
} |
|
a**********0 发帖数: 422 | 42 超时的递归方法
public class ReorderListSlow {
public void reorderList(ListNode head) {
if(head == null)
return;
if(head.next ==null)
return;
if(head.next.next == null)
return;
ListNode scanner = head;
int length =0;
while(scanner != null){
length++;
scanner = scanner.next;
}
head = reorderHelper(head, length);
... 阅读全帖 |
|
p****6 发帖数: 724 | 43
doubly
.
if(node == null) return node;
Stack stack = new Stack();
ListNode head = new ListNode(-1);
head.next = node;
while(node!=null){
if(node.other!=null){
if(node.next!=null)
stack.add(node.next);
node.next = node.other;
node.other = null;
}else{
if(node.next == null && !stack.isEmpty()){
L... 阅读全帖 |
|
p*y 发帖数: 108 | 44 店面是两个中国人,一开始知道是国人还比较欣喜. 结果证明完全不是这么回事,反而感
觉很严格,最终挂了. 请大家分析下为啥挂? 难道第二题没有按面试官心中理想的答案
在面试时给他写出来? 以后看来一定要注意时间.
1. two sum
一开始根据题目理解以为是排好序的数组, 于是从两头开始找:
boolean twoSum(int[] nums, int sum){
if(nums==null || nums.length<2)
return false;
int low = 0, high = nums.length-1;
while(low
if( (nums[low]+nums[high]) == sum ){
return true;
}else if((nums[low]+nums[high]) < sum){
low++;
}else{
... 阅读全帖 |
|
S*******C 发帖数: 822 | 45 /**
* 10.3 Given an input file with four billion non-negative integers, provide
an algorithm
to generate an integer which is not contained in the file. Assume you have
1 GB of
memory available for this task.
FOLLOW UP
What if you have only 10 M8 of memory? Assume that all the values are
distinct and
we now have no more than one billion non-negative integers.
CC Page 347
*/
import java.io.*;
import java.util.Scanner;
//A bit array (also known as bitmap, bitset, bit string, or bit vector) is
an ar... 阅读全帖 |
|
I**********s 发帖数: 441 | 46 简单:
TreeNode * convert(TreeNode * root) {
if (! root) return NULL;
TreeNode * head = NULL, * tail = NULL;
stack s;
TreeNode * n = root;
while (1) {
while (n) {
s.push(n);
n = n->left;
}
if (s.empty()) {
if (head) {
head->left = tail;
tail->right = head;
}
break;
}
... 阅读全帖 |
|
I**********s 发帖数: 441 | 47 简单:
TreeNode * convert(TreeNode * root) {
if (! root) return NULL;
TreeNode * head = NULL, * tail = NULL;
stack s;
TreeNode * n = root;
while (1) {
while (n) {
s.push(n);
n = n->left;
}
if (s.empty()) {
if (head) {
head->left = tail;
tail->right = head;
}
break;
}
... 阅读全帖 |
|
e***a 发帖数: 1661 | 48 class Solution {
private DLNode head = null, dlnode = null;
private void bt2dll(TreeNode tnode) {
if (tnode == null) return null;
bt2dll(tnode.left);
if (dlnode == null) {
dlnode = new DLNode(tnode.val);
head = dlnode;
} else {
dlnode.next = new DLNode(tnode.val);
dlnode.next.prev = dlnode;
dlnode = dlnode.next;
}
bt2dll(tnode.right);
}
public DLNode start(TreeNode tnode) {... 阅读全帖 |
|
l*****n 发帖数: 246 | 49 贴个解法:
public void upSideDown(Node root, Node leaf) {
if(root == leaf || root==null){return;}
if(root.left!=null && containsLeaf(root.left, leaf)) {
Node temp = root.left;
root.left = null;
upSideDown(temp, leaf);
temp.right = root;
}
if(root.right!=null && containsLeaf(root.right, leaf)) {
Node temp = root.right;
root.right = null;
upSideDown(temp, leaf);
temp.left = root;
}
}
private boolean containsLeaf(Node ro... 阅读全帖 |
|
a*********8 发帖数: 140 | 50 用recursion和post order的stack,做第一题:
class TreeNode2 {
char val;
TreeNode2 left;
TreeNode2 right;
TreeNode2(char val){
this.val = val;
}
}
public class Exercise2 {
public List pathRootToLeaf(TreeNode2 root){
List list = new ArrayList();
if (root == null){
return list;
}
List left = pathRootToLeaf(root.left);
List right = pathRootToLeaf(root.right);
i... 阅读全帖 |
|