部分middle难度leetcode题解法

//判断是否时same二叉树
class Solution
{
public:
void Preorder (TreeNode * t, vector < char > v)
{
if (t == NULL )
v. push_back ( '#' );
else
{
v. push_back (t-> val + '0' );
Preorder (t-> left , v);
Preorder (t-> right , v);
}
}
bool isSameTree (TreeNode * p, TreeNode * q)
{
vector < char > v1;
vector < char > v2;
Preorder (p, v1);
Preorder (q, v2);
if (v1. size () != v2. size ())
{
return ( false );
}
else
{
for ( int i = 0 ; i < v1. size (); i ++ )
{
if (v1[i] != v2[i])
{
return ( false );
}
}
}
return ( true );
}
};
bool isSameTree (TreeNode * p, TreeNode * q)
{
if (p == NULL && q == NULL )
return ( true );
else if ((p == NULL && q != NULL ) || (p != NULL && q == NULL ))
return ( NULL );
else
return (p-> val == q-> val && isSameTree (p-> left , q-> left ) && isSameTree (p-> right , q-> right ));
}

//leetcode左节点叶子之和
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution
{
public:
int dfs (TreeNode *& root, int & flag)
{
if (root)
{
int sum = 0 ;
if (flag == 1 && root-> left == NULL && root-> right == NULL )
sum += root-> val ; //左节点相加
sum = sum + dfs (root-> left , 1 ) + dfs (root-> right , 0 );
return (sum);
}
else
return ( 0 );
}
int sumOfLeftLeaves (TreeNode * root)
{
int sum = 0 ;
dfs (root, 0 );
return (sum);
}
};
//remove the element
ListNode * removeElements (ListNode * head, int val)
{
if (head)
{
if (head-> val == val) //头节点
{
while (head && head-> val == val)
{
head = head-> next ;
}
}
if (head)
{
ListNode * front = head; //此处保证第一个节点并不是想要的
ListNode * cur = head-> next ;
while (cur)
{
if (cur-> val == val)
{
front-> next = cur-> next ;
cur = front-> next ;
}
else
{
cur = cur-> next ;
front = front-> next ;
}
}
return (head);
}
else
return ( NULL );
}
else
return ( NULL );
}

//判断回文
//Could you do it in O(n) time and O(1) space?
class Solution
{
public:
ListNode * reverse (ListNode * head)
{
ListNode * p = head;
ListNode * temp = head-> next ;
ListNode * tail = NULL ;
while (temp)
{
p-> next = tail;
tail = p; //尾部指向P
p = temp;
temp = p-> next ;
}
p-> next = tail;
return (p);
}
//判断是否是回文
bool isPalindrome (ListNode * head)
{
ListNode * p = head;
int length = 0 ;
while (p)
{
p = p-> next ;
length ++ ;
}
ListNode * q = head;
int i = 0 ;
while (i != (length / 2 + 1 ))
{
q = q-> next ;
i ++ ;
}
ListNode * middle = reverse (q); //求出倒置的后半部分链表
ListNode * cur = head;
i = 0 ;
while (i <= length / 2 )
;
{
if (middle-> val != cur-> next )
return ( false );

middle = middle-> next ;
cur = cur-> next ;
i ++ ;
}
return ( true );
}
};

<!-- Intersection of Two Linked Lists -->
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution
{
public:
ListNode * getIntersectionNode (ListNode * headA, ListNode * headB)
{
if (headA && headB)
{
ListNode * pa = headA;
ListNode * pb = headB;
int length_a = 1 ;
int length_b = 1 ;
while (pa-> next )
{
length_a ++ ;
pa = pa-> next ;
}
while (pb-> next )
{
length_b ++ ;
pb = pb-> next ;
}
if (pa == pb) //若最后的节点是一样的则证明有交集
{
int i = 1 ;
//找出最长的
if (length_a >= length_b)
{
ListNode * cur = headA;
while (i <= (length_a - length_b))
{
cur = cur-> next ;
i ++ ;
} //从长的链表开始
ListNode * qa = headA;
ListNode * qb = headB;
while (qa != qb)
{
qa = qa-> next ;
qb = qb-> next ;
}
return (qa);
}
else
{
}
}
else
return ( NULL );
}
else
return ( NULL );
}
};

class Solution
{
public:
ListNode * getIntersectionNode (ListNode * headA, ListNode * headB)
{
if (headA && headB)
{
vector < ListNode *> Node;
Node. push_back (headA);
Node. push_back (headB);
vector < int > L; //记录长度
L. push_back ( 1 );
L. push_back ( 1 );
ListNode * pa = headA;
ListNode * pb = headB;
//判断最后的节点是否一致
while (pa-> next )
{
L[ 0 ] ++ ;
pa = pa-> next ;
}
while (pb-> next )
{
L[ 1 ] ++ ;
pb = pb-> next ;
}
if (pa == pb) //如果最后的是相同节点则就是有交集
{
if (leangth_a >= length_b)
{
index_long = 0 ;
index_short = 1 ;
}
else
{
index_long = 1 ;
index_short = 0 ;
}
int i = 1 ;
//q节点是长的头结点
ListNode * cur = Node[index_long];
while (i <= math. abs (length_a - length_b))
{
cur = cur-> next ;
i ++ ;
}
//cur移动到|la-lb|的位置 此时cur q距离intersection的位置一样
ListNode * q = Node[index_short];
while (q != cur)
{
q = q-> next ;
cur = cur-> next ;
}
return (q);
}
else
return ( NULL );
}
else
return ( NULL );
}
};

//Binary Tree Tilt
class Solution
{
public:
//一个节点的和
int SumInorder (TreeNode * root)
{
if (root)
return (root-> val + SumInorder (root-> left ) + SumInorder (root-> right ));
else
return ( 0 );
}
//左右节点的差值
int Deference (TreeNode * root)
{
if (root)
return ( abs ( SumInorder (root-> left ) - SumInorder (root-> right )));
else
return ( 0 );
}
//找出
int findTilt (TreeNode * root)
{
if (root)
return ( Deference (root) + findTilt (root-> left ) + findTilt (root-> right ));
else
return ( 0 );
}
};

//Linked List Components
class Solution
{
public:
bool isIn (vector < int > & G, ListNode * p) //在就返回true
{
vector < int > ::iterator it;
for (it = G. begin (); it != G. end (); it ++ )
{
if (( * it) == p-> val )
return ( true );
}
return ( false );
}
int numComponents (ListNode * head, vector < int > & G)
{
if (head)
{
int count = 0 ;
int i = 0 ;
ListNode * p = head;
while (p)
{
if ( isIn (G, p)) //发现相等
{
count ++ ; //发现相等加一
while (p && isIn (G, p)) //如果在数组中
p = p-> next ;
}
if (p)
p = p-> next ; //向下移动
else
break ;
}
return (count);
}
return ( 0 );
}
};
q -> next = p -> next;
p -> next = q;

p -> left -> right = p;

p -> l -> r = p -> r
p -> r -> l = p -> l

//Swap Nodes in Pairs

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/

class Solution
{
public:
ListNode * swapPairs (ListNode * head)
{
ListNode *
}
};

// Swap Nodes in Pairs

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution
{
public:
ListNode * swapPairs (ListNode * head)
{
if (head)
{
ListNode * p = head;
if (p-> next != NULL )
{
int temp;
while (p-> next != NULL )
{
temp = p-> value ;
p-> value = p-> next -> value ;
p-> next -> value = temp;
p = p-> next ;
}
}
}

return ( NULL );
}
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值