Wrath Brush Leetcode (1) 237, 104, 136, 100

Source: Internet
Author: User

https://leetcode.com/problemset/algorithms/the above topic, do several questions every day, generally from the accuracy rate high to low to do

Programming language is C language, because the fastest running ...


237 Delete Node in a Linked List 47.8% Easy
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.

Supposed the linked list 1 -> 2 -> 3 -> 4 is and you were given the third node with value 3 , the linked list should become 1 -> 2 -> 4 a Fter calling your function.

/** * Definition for singly-linked list. * struct ListNode {*     int val; *     struct ListNode *next; *}; */void deletenode (struct listnode* node) {    if (nod E->next = NULL) {        node->val = node->next->val;        node->next=node->next->next;       }}
To delete a given node, simply change Val of the current node to the value of the next node, which is equivalent to two nodes of the same value, then point the next pointer of the current node to the next node, that is, to skip the second identical node.



104 Maximum Depth of Binary Tree 45.1% Easy

Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along, the longest path from the root node to the farthest leaf node.

/** * Definition for a binary tree node. * struct TreeNode {*     int val; *     struct TreeNode *left; *     struct TreeNode *right; *}; */int maxDepth (struct treenode* root) {    if (root = NULL)//Recursive exit          return 0;      int depthleft = maxDepth (root->left);      int depthright = maxDepth (root->right);      return depthleft > Depthright? (Depthleft + 1): (Depthright + 1);  }
To find the depth of the binary tree, using the idea of recursion, the root would like to do ordinary nodes, any node depth is the left and right node deeper than the value of +1, in addition to the leaves (the depth of 0, is also the end of recursion condition)


136 Single number 45% Medium

Given an array of integers, every element appears twice except for one. Find the single one.

Note:
Your algorithm should has a linear runtime complexity. Could you implement it without using extra memory?

int Singlenumber (int* nums, int numssize) {        if (Nums = = 0 | | numssize < 1)              return 0;          int key = Nums[0];          for (int i = 1; i < numssize; ++i) {              key ^= nums[i];          }          Return key;  }
The difficulty is that the array size increases and the run time only increases linearly.

All elements of an array are XOR, and the same element is equal to 0, and the only element with 0 XOR equals itself.

100 Same Tree 41.5% Easy

Given binary trees, write a function to check if they is equal or not.

The binary trees is considered equal if they is structurally identical and the nodes has the same value.

/** * Definition for a binary tree node. * struct TreeNode {*     int val; *     struct TreeNode *left; *     struct TreeNode *right; *}; */bool Issametree (Struc T treenode* p, struct treenode* q) {    if (!p &&!q) return true;  Null together, the Same,skip the remaining code    if (!p | |!q) return false;//one is null and the other are not    re Turn (P->val = = q->val) && issametree (P->left, Q->left) && issametree (P->right, q-> right);     }
Similar to the 104 question, using recursive thinking, the binary tree is the same, which means that starting from the root node, must conform to the corresponding node of the same value + the left node is the root node of the same tree + the right node is the same tree as the root node

First of all, the!p&&!q is a leaf node, two are the same tree

If it is not a leaf node at the same time, determine if either side is a leaf node, and if so, then they are not the same tree

Run the algorithm on the root node to produce results





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Wrath Brush Leetcode (1) 237, 104, 136, 100

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.